简体   繁体   English

laravel route.php更多的控制器动作方法

[英]laravel route.php more controller actions methods

I have three routes: 我有3条路线:

Route::get('{project}', 'ProjectController@showProject')
->where('project', '[A-Za-z0-9-]+');            

Route::get('{project}/{module}', 'ProjectController@showModule')
->where('module', '[A-Za-z0-9-]+');

Route::get('{project}/{module}/{submodule}/{resources}',   'ProjectController@showGraphsResources')
->where(array('submodule' => '[A-Za-z0-9-]+','resource', '[A-Za-z0-9-]+'));

only function in projectController is different projectController唯一的功能是不同的

How can I made just one route with different actions? 如何只用一条路线进行不同的操作?

Somethink like this... (which is not correct) 像这样...(不正确)

Route::get('{project}/{module}/{submodule}/{resources}', 'ProjectController@showProject' 'ProjectController@showModule','ProjectController@showGraphsResources',)
->where(array('submodule' => '[A-Za-z0-9-]+','resource', '[A-Za-z0-9-]+'));

I think: 我认为:

Route::get('{project}/{module}/{submodule}/{resources}', 'ProjectController@showGraphsResources')->where(array('submodule' => '[A-Za-z0-9-]+','resource', '[A-Za-z0-9-]+'));

Route::get('{project}/{module}', 'ProjectController@showModule')->where('module', '[A-Za-z0-9-]+');

Route::get('{project}', 'ProjectController@showProject')->where('project', '[A-Za-z0-9-]+');

should work. 应该管用。

It looks a bad idea to use one route for multiple actions (IMO) but... you may try something like this: 使用一条路由执行多个操作(IMO)似乎是一个坏主意,但是...您可以尝试执行以下操作:

Route::get(
    '{project}/{module?}/{submodule?}/{resources?}',
    function($project, $module = null, submodule = null, $resources = null) {
        if(!is_null($project)) {
            $pc = App::make('ProjectController');
            if(is_null($module)) return $pc->showProject($project);
            else {
                if(is_null($submodule)) return $pc->showModule($project, $module);
                else {
                    if(!is_null($resources)) return $pc->showGraphsResources($project, $module, $submodule, $resources);
                }
            }
        }
    }
);

Now in your ProjectController create three methods like this: 现在在您的ProjectController创建如下三个方法:

class ProjectController extends BaseController {

    public function showProject($project)
    {
        //...
    }

    public function showModule($project, $module)
    {
        //...
    }

    public function showGraphsResources($project, $module, $submodule, $resources)
    {
        //...
    }
}

Alternatively you may use only one missingMethod to catch all methods in a controller, for example: 或者,您可以仅使用一个missingMethod来捕获控制器中的所有方法,例如:

class ProjectController extends BaseController {

    public function missingMethod($args = array())
    {
        // Now check the $args passed,
        // depending on the $args you
        // may take an action, try dd($args)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM