Search code examples
laravellaravel-4laravel-routing

How to pass default parameters to laravel controller in a get route


I have a route like that:

Route::get('category/{id}/{date?}', array('as' => 'category/date', 'uses' => 'Controller@getCategory'));

I want to run @getCategory with default parameters when called '/' root route. So if '/' route called, getCategory function should run with id=1 and date=2015-12-18.

How should I do that?


Solution

  • Register it as a separate route:

    Route::get('/', 'Controller@getCategory')->named('home');
    Route::get('category/{id}/{date?}', 'Controller@getCategory')->named('category/date');
    

    Then in your controller, set default values for those arguments:

    public function getCategory($id = 1, $date = '2015-12-18')
    {
        // do your magic...
    }