Domanda

I have three routes:

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

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-]+'));
È stato utile?

Soluzione

It looks a bad idea to use one route for multiple actions (IMO) but... you may try something like this:

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:

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:

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)
    }
}

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top