Laravel - why not a relative URL?

I studied Laravel recently, and it seems I missed a key point: why should relative relationships be avoided?

For example, I was offered to use URL::to()one that displays the full path to the page passed as a parameter, but why do this when you can just insert a relative link? For example, posting URL::to('my/page')in <href>will simply paste http://www.mywebsite.com/my/pagein <href>; but on my site it href='my/page'works exactly the same. On my website, I based all the relative URLs from a file index.phpfound in the public directory.

Clearly, I miss the key point why full paths are used.

+4
source share
2 answers

I found that using route()in the named routes is much better practice. If at some point you decide, for example, that your admin panel should not point to example.com/admin, but example.com/dashboard, you will have to sift through all the code to find all the links to Url::to("/admin"). With the named routes, you just need to change the link inroutes.php

Example:

Route::get('/dashboard', ['as' => 'admin', 'uses' => 'AdminController@index']);

Now every time you need to provide a link to your admin page, just do the following:

<a href="{{ route('admin') }}">Admin</a>

Much better, in my opinion.

It is even available in your backend, for example, in AdminController.php

// do stuff
return redirect()->route('admin');

http://laravel.com/docs/5.1/routing#named-routes

+4
source

, - :

Route::get('my/page', ['as' => 'myPage', function () {
    // return something
}]);

Route::get('my/page', 'FooController@showPage')->name('myPage');

, URL::route() ( route() L5), Blade, .

, , , .

+3

Source: https://habr.com/ru/post/1617474/


All Articles