Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reorder or ignore parameters in controller routes?

The question title is the most explicit I could think of, but here's a use case/example for clarity's sake:

Say I define the following route to show an article:

Route::get('article/{slug}/{id}', 'ArticleController@show');

...

class ArticleController extends BaseController {

    public function show($id)
    {
        return View::make('article')->with('article', Article::find($id));
    }

}

This won't work, as show will misake the $id parameter with the $slug parameter. Is there a way to pass only the $id parameter to the show method?

like image 937
Lazlo Avatar asked Oct 20 '25 09:10

Lazlo


2 Answers

I dont know if you still look for solution or not, but as I had the same problem and I didn't like these solutions, I did this:

in the your ArticleController you overload the callAction($method, $parameters) method, this is a method in Laravel controller class, so it looks like this:

public function callAction($method, $parameters)
{
        unset($parameters['id']);
        unset($parameters['slug']);

        return parent::callAction($method, $parameters);
}

after this you ca easily do this:

public function show($id)
{
    return View::make('article')->with('article', Article::find($id));
}
like image 80
Torgheh Avatar answered Oct 22 '25 00:10

Torgheh


It's possible to manually call controller functions:

Route::get('article/{slug}/{id}', function($slug, $id)
{
    return App::make('ArticleController')->show($id);
});
like image 25
Lazlo Avatar answered Oct 22 '25 01:10

Lazlo