Can somebody explain the syntax of the Laravel 4 UrlGenerator class? I can't find it in the documentation.
I have the following route:
Route::resource('users', 'UsersController');
It took me long time to figure out that this:
{{ Url::action('UsersController@show', ['users' => '123']) }}
generates the desired html:
http://localhost/l4/public/users/123
I looked in UrlGenerator.php
/**
* Get the URL to a controller action.
*
* @param string $action
* @param mixed $parameters
* @param bool $absolute
* @return string
*/
public function action($action, $parameters = array(), $absolute = true)
..but that doesn't really bring me further.
What can I pass as $parameters
?
I now know that ['users' => '123']
works, but what's the background of this? And are there other ways of passing data?
You can pass query string to URL in laravel using named route and controller action. You can pass query string as comma separated array to named route and controller action and redirect to URL.
To retrieve the query parameters on your Laravel backend, you can make use of either the "Request" class or the "request()" helper method. Imagine you want to get the "search" query from the URL, you can do as follows. $searchQuery = $request->query('search');
For a start it is using a named route. This means that the link between the form and its controller are not dictated by the url that the user sees. The next advantage is that you can pass additional parameters into the route helper and it will put those in the correct place in the form action.
Laravel Basics Routing in Laravel allows you to route all your application requests to its appropriate controller. The main and primary routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure, given that it should have to be a simple and expressive way of routing.
You aren't actually required to give the name of the parameter as the key of the array. The replacements will happen from left to right if no names are provided, as far as I can remember.
As an example, your resource controllers route definition will look something like this:
/users/{users}
So, a URL generated like URL::action('UsersController@show', ['123'])
will generate the URL localhost/project/public/users/123
, much like it has already for you.
So what you're passing in are the parameters required for the URL to be generated correctly. If the resource was nested, a definition might look something like this.
/users/{users}/posts/{posts}
To generate a URL you'd need to pass both the user ID and the post ID.
URL::action('UsersPostsController@show', ['123', '99']);
The URL would look something like localhost/project/public/users/123/posts/99
Well there is a better way of generating URLs when working with resources.
URL::route('users.index') // Show all users links to UserController@index
URL::route('users.show',$user->id) // Show user with id links to UserController@show($id)
URL::route('users.create') // Show Userform links to UserController@create
URL::route('users.store') // Links to UserController@store
URL::route('users.edit',$user->id) // Show Editform links to UserController@edit($id)
URL::route('users.update',$user->id) // Update the User with id links to UserController@update($id)
URL::route('users.destroy',$user->id) // Deletes a user with the id links to UserController@destroy
Hope that clears things up. Some Documentation on this can be found here http://laravel.com/docs/controllers#resource-controllers
For those using PHP 5.3, this should be:
URL::action('UsersController@show', array('123') )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With