What's the recommended approach for declaring routes: with a forward slash or is it better to leave it out? Are there any benefits to using one over the other or is it just a matter of preference?
Is it better to use this:
Route::get('/read', function(){
$user = User::findOrFail(1);
return $user;
});
Or this instead:
Route::get('read', function(){
$user = User::findOrFail(1);
return $user;
});
Thanks in advance.
It comes down to preference. When passing the route, it actually trims the forward slashes off, then formats it properly. In Illuminate/Routing/Router.php, all routes go through the prefix
function, which looks like this:
protected function prefix($uri)
{
return trim(trim($this->getLastGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/';
}
So if you create a group prefix /test/
and and a uri of /route
, it becomes test/route
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With