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

Kiran Subedi picture Kiran Subedi · Mar 16, 2015 · Viewed 19.1k times · Source

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 

Answer

Limon Monte picture Limon Monte · Mar 16, 2015

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.