Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "as" keyword mean in Laravel routing?

As I understand from the Laravel documentation, it's used for redirection, but maybe I'm wrong.

I wrote

Route::get('user/profile', ['as' => 'profile', function () {
    echo 'some_text';
}]);

then I was expecting my URL to redirect from https://base_url/public/index.php/user/profile to https://base_url/public/index.php/profile but it doesn't happen.

Overall, I want to know, what the difference is if I used

Route::get('user/profile', function () {
    echo 'some_text';
});

instead of the above routing rule.

like image 639
Aniket Singh Avatar asked Jun 16 '15 07:06

Aniket Singh


1 Answers

The purpose isn't for re-direction in your routing file.

Instead, with the example route you provided, Laravel will allow you to reference said route by using:

$url = route('profile');

So you don't have to build the URL manually over and over again in your code.

So, in short: the difference is the first thing is a named route, and the last thing is a non-named route. Since you called the first route, you can reference it by that name.

like image 178
Nicklas Kevin Frank Avatar answered Oct 09 '22 22:10

Nicklas Kevin Frank