Search code examples
phplaravel-5.1laravelcollective

How can I build a url with a named route?


I'm trying to generate a url link using a named route. For example, I have http://myapp.com/{token}. But, I had been using a lot of function with route but the result that I have is http://myapp.com?token instead of http://myapp.com/token.

What can I do?

The only way to generate the good url is using the function url without the name of url. For example: url( '/reset', [$token]). But in this case, I am not using the name of the route where that is what I would like to use.

Real problem: I have a route like this:

// Password reset routes...
Route::get('reset/{token}', [
    'as' => 'password.reset',
    'uses' => 'Auth\PasswordController@getReset'
]);

A blade template:

<a href="{{ url( '/reset', [$token]) }}">Click here to reset your password</a>
<br>
<a href="{{ url( route('password.reset', $token)) }}">Click here to reset your password</a>
<br>
{!! link_to_route('password.reset', 'Click here to reset your password', $token) !!}
<br>
<a href="{{ route('password.reset', $token) }}">Click here to reset your password</a>

The result is:

<a href="https://laravel51.app2/reset/72a560d23a1bf17eec336d07c169bd4c02d25e24ae3581977b4fa4aa4f4e3252">Click here to reset your password</a>
<br>
<a href="https://laravel51.app2/reset?72a560d23a1bf17eec336d07c169bd4c02d25e24ae3581977b4fa4aa4f4e3252">Click here to reset your password</a>
<br>
<a href="https://laravel51.app2/reset?72a560d23a1bf17eec336d07c169bd4c02d25e24ae3581977b4fa4aa4f4e3252">Click here to reset your password</a>
<br>
<a href="https://laravel51.app2/reset?72a560d23a1bf17eec336d07c169bd4c02d25e24ae3581977b4fa4aa4f4e3252">Click here to reset your password</a>

Solution

  • route('password.reset', [ 'token' => $token ])
    

    is what you need to write