控制器分组路由


目的

降低控制器复杂度

我有个Volunteer(自愿者)控制器,下面有多个子模块,比如:job、question、resume、group

  1. app
  2. |--cli
  3. |--m
  4. |--web
  5. | |--controller
  6. | | |--Message.php
  7. | | |--Passport.php
  8. | | |--User.php
  9. | | |--Volunteer.php

如果不分组,那么,所有子模块的操作(方法)都写到Volunteer这一个控制器里面,方法一多,就感觉很混乱

访问方式:

  1. http://www.timophp.com/volunteer/jobFind/
  2. http://www.timophp.com/volunteer/jobDetail/
  3. http://www.timophp.com/volunteer/jobPublish/
  4. http://www.timophp.com/volunteer/jobUpdate/
  5. http://www.timophp.com/volunteer/questionPublish/
  6. http://www.timophp.com/volunteer/questionDetail/

用了控制器分组:

  1. app
  2. |--cli
  3. |--m
  4. |--web
  5. | |--controller
  6. | | |--volunteer
  7. | | | |--job.php
  8. | | | |--group.php
  9. | | | |--question.php
  10. | | | |--resume.php
  11. | | |--Message.php
  12. | | |--Passport.php
  13. | | |--User.php
  14. | | |--Volunteer.php

就分成了多子控制器,相应的方法就分散到了子控制器,Volunteer控制器就显得很简洁,而且分组之后显得更清晰明了

  1. http://www.timophp.com/volunteer/job/find/
  2. http://www.timophp.com/volunteer/job/detail/
  3. http://www.timophp.com/volunteer/job/publish/
  4. http://www.timophp.com/volunteer/job/update/
  5. http://www.timophp.com/volunteer/question/publish/
  6. http://www.timophp.com/volunteer/question/detail/

怎样配置控制器分组

只需在配置文件中配置controller这一项,如:

  1. 'controller' => [
  2. 'volunteer/group' => \app\web\controller\volunteer\Group::class,
  3. 'volunteer/job' => \app\web\controller\volunteer\Job::class,
  4. 'volunteer/question' => \app\web\controller\volunteer\Question::class,
  5. 'volunteer/resume' => \app\web\controller\volunteer\Resume::class,
  6. ],