简体   繁体   中英

how to add optional parameter to laravel named routes?

Using Laravel 4.2 and according to routing documentation

We can define a named route as

Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController@showProfile'));

And define an optional parameter with this other way

Route::get('user/{name?}', function($name = null)
{
    return $name;
});

I want to add an optional parameter to a named route. How to combine both ?

Try this

Route::get('user/{name?}', function($name = null)
{
    return $name;
})->name('foo');

Update

sorry the name method not exists in Laravel 4.2 You can do it in another way

Route::get('user/profile/{name?}', array('as' => 'profile', 'uses' => 'UserController@showProfile'))

or

Route::get('user/profile/{name?}', array('as' => 'profile', function($name = null) {
// your code here 
})

You can define route common for all functions.. like:

Route::controller('uses', 'UserController');

And define function with optional parameters:

  public function getView($param = 0)
     //your code here
   }

using this you can use optional parameters in a function on which you required.with help of ajax call on function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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