Search code examples
phplaravellaravel-5laravel-routing

Difference between URL::to and URL::route in laravel


What is the difference between

<a href=" {{ URL::route('/account/register') }}" >Register 1 </a>

and

<a href=" {{ URL::to('/account/register') }}" >Register 2 </a>

I defined the routes.php as

Route::get('/account/register','RegisterController@create');

When I click on 'Register 1' I got the following error

Route [/account/register] not defined.

But when I click on 'Register 2' ,it goes to the

RegisterController@create 

Solution

  • URL::route gets the URL to a named route. So in your case, if you name your route like this:

    Route::get('/account/register', [
        'name' => 'register', 
        'uses' => 'RegisterController@create'
    ]);
    

    then you will be able to use

    <a href="{{ URL::route('register') }}" >Register 1</a>
    

    in blade templates.