控制器分组路由

 


 

目的

降低控制器复杂度

我有个Volunteer(自愿者)控制器,下面有多个子模块,比如:job、question、resume、group
app
  |--cli
  |--m
  |--web
  |   |--controller
  |   |   |--Message.php
  |   |   |--Passport.php
  |   |   |--User.php
  |   |   |--Volunteer.php

 

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

访问方式:

 

http://www.timophp.com/volunteer/jobFind/
http://www.timophp.com/volunteer/jobDetail/
http://www.timophp.com/volunteer/jobPublish/
http://www.timophp.com/volunteer/jobUpdate/
http://www.timophp.com/volunteer/questionPublish/
http://www.timophp.com/volunteer/questionDetail/

 

用了控制器分组:

 

app
  |--cli
  |--m
  |--web
  |   |--controller
  |   |   |--volunteer
  |   |   |   |--job.php
  |   |   |   |--group.php
  |   |   |   |--question.php
  |   |   |   |--resume.php
  |   |   |--Message.php
  |   |   |--Passport.php
  |   |   |--User.php
  |   |   |--Volunteer.php

 

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

 

http://www.timophp.com/volunteer/job/find/
http://www.timophp.com/volunteer/job/detail/
http://www.timophp.com/volunteer/job/publish/
http://www.timophp.com/volunteer/job/update/
http://www.timophp.com/volunteer/question/publish/
http://www.timophp.com/volunteer/question/detail/

 

怎样配置控制器分组

 

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

 

'controller' => [
        'volunteer/group' => \app\web\controller\volunteer\Group::class,
        'volunteer/job' => \app\web\controller\volunteer\Job::class,
        'volunteer/question' => \app\web\controller\volunteer\Question::class,
        'volunteer/resume' => \app\web\controller\volunteer\Resume::class,
    ],