简体   繁体   中英

What is the syntax for named parameters in Laravel 4 routes?

I would like to use named parameters for all my routes in Laravel 4, rather than relying on the position in the route definition. I cannot find anything in the manual that describes how to do this.

I saw this github issue that indicates this feature exists in Laravel 4: https://github.com/laravel/laravel/pull/832#issuecomment-8217765

I've tried setting up routes with a colon prefix, eg

Route::get('{:foo}/{:bar}', function($bar, $foo) {})

I also tried using what appears to be the old syntax, with (:foo)/(:bar) rather than curly braces, but that doesn't work either. The parameters always come through in the order they were defined in the route.

Example:

The application I'm working supports some routes that I need to maintain. But they were not designed in a proper restful style.

/autoaccount/user/2/3 refers to to the auto account with id=2 of the user with id=3

The new design for this is:

/users/{userid}/autoaccounts/{autoaccountid}

So the parameters are in a different order for the new route. I would like to point both of these routes to the same function.

Laravel 4 supports this as you said.

You need the variables in curly brackets as you have done, but without the colon :

Docs are routing are here .

Relevant example:

Route::get('user/{id}/{name}', function($id, $name)
{
    //
})
->where(array('id' => '[0-9]+', 'name' => '[a-z]+'))

Note that the where() portion is optional.

Check out the docs linked above for available options. Note that you can define a controller method and other options in an array passed to the Route::method() calls.

Lastly, see also the controller docs for routing to a controller.

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