In Routes, you can create a group within a group, assigning a certain middleware only to some URLs in the "parent" group.
Route::group(['prefix' => 'account', 'as' => 'account.'], function() { Route::get('login', 'AccountController@login'); Route::get('register', 'AccountController@register'); Route::group(['middleware' => 'auth'], function() { Route::get('edit', 'AccountController@edit'); });});
You can create route group by dynamic subdomain name, and pass its value to every route.
Route::domain('{username}.workspace.com')->group(function () { Route::get('user/{id}', function ($username, $id) { // });});
If you use Laravel UI package, you likely want to know what routes are actually behind Auth::routes()
?
You can check the file /vendor/laravel/ui/src/AuthRouteMethods.php
.
public function auth(){ return function ($options = []) { // Authentication Routes... $this->get('login', 'Auth\LoginController@showLoginForm')->name('login'); $this->post('login', 'Auth\LoginController@login'); $this->post('logout', 'Auth\LoginController@logout')->name('logout'); // Registration Routes... if ($options['register'] ?? true) { $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register'); $this->post('register', 'Auth\RegisterController@register'); } // Password Reset Routes... if ($options['reset'] ?? true) { $this->resetPassword(); } // Password Confirmation Routes... if ($options['confirm'] ?? class_exists($this->prependGroupNamespace('Auth\ConfirmPasswordController'))) { $this->confirmPassword(); } // Email Verification Routes... if ($options['verify'] ?? false) { $this->emailVerification(); } };}
The default use of that function is simply this:
Auth::routes(); // no parameters
But you can provide parameters to enable or disable certain routes:
Auth::routes([ 'login' => true, 'logout' => true, 'register' => true, 'reset' => true, // for resetting passwords 'confirm' => false, // for additional password confirmations 'verify' => false, // for email verification]);
Tip is based on suggestion by MimisK13
You can do Route model binding like Route::get('api/users/{user}', function (App\User $user) { … }
- but not only by ID field. If you want {user}
to be a username
field, put this in the model:
public function getRouteKeyName() { return 'username';}
This thing was optional before Laravel 8, and became a standard main syntax of routing in Laravel 8.
Instead of routing like this:
Route::get('page', 'PageController@action');
You can specify the Controller as a class:
Route::get('page', [\App\Http\Controllers\PageController::class, 'action']);
Then you will be able to click on PageController in PhpStorm, and navigate directly to Controller, instead of searching for it manually.
Or, to make it shorter, add this to top of Routes file:
use App\Http\Controllers\PageController;// Then:Route::get('page', [PageController::class, 'action']);
If you want to specify additional logic for not-found routes, instead of just throwing default 404 page, you may create a special Route for that, at the very end of your Routes file.
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () { Route::get('/home', 'HomeController@index'); Route::resource('tasks', 'Admin\TasksController');});// Some more routes....Route::fallback(function() { return 'Hm, why did you land here somehow?';});
We can validate parameters directly in the route, with “where” parameter. A pretty typical case is to prefix your routes by language locale, like fr/blog
and en/article/333
. How do we ensure that those two first letters are not used for some other than language?
routes/web.php
:
Route::group([ 'prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}']], function () { Route::get('/', 'HomeController@index'); Route::get('article/{id}', 'ArticleController@show');});
You can limit some URL to be called a maximum of 60 times per minute, with throttle:60,1
:
Route::middleware('auth:api', 'throttle:60,1')->group(function () { Route::get('/user', function () { // });});
But also, you can do it separately for public and for logged-in users:
// maximum of 10 requests for guests, 60 for authenticated usersRoute::middleware('throttle:10|60,1')->group(function () { //});
Also, you can have a DB field users.rate_limit and limit the amount for specific user:
Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () { Route::get('/user', function () { // });});
If you pass additional parameters to the route, in the array, those key / value pairs will automatically be added to the generated URL's query string.
Route::get('user/{id}/profile', function ($id) { //})->name('profile');$url = route('profile', ['id' => 1, 'photos' => 'yes']); // Result: /user/1/profile?photos=yes
If you have a set of routes related to a certain "section", you may separate them in a special routes/XXXXX.php
file, and just include it in routes/web.php
Example with routes/auth.php
in Laravel Breeze by Taylor Otwell himself:
Route::get('/', function () { return view('welcome');});Route::get('/dashboard', function () { return view('dashboard');})->middleware(['auth'])->name('dashboard');require __DIR__.'/auth.php';
Then, in routes/auth.php
:
use App\Http\Controllers\Auth\AuthenticatedSessionController;use App\Http\Controllers\Auth\RegisteredUserController;// ... more controllersuse Illuminate\Support\Facades\Route;Route::get('/register', [RegisteredUserController::class, 'create']) ->middleware('guest') ->name('register');Route::post('/register', [RegisteredUserController::class, 'store']) ->middleware('guest');// ... A dozen more routes
But you should use this include()
only when that separate route file has the same settings for prefix/middlewares, otherwise it's better to group them in app/Providers/RouteServiceProvider
:
public function boot(){ $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); // ... Your routes file listed next here });}
If you use resource controllers, but want to change URL verbs to non-English for SEO purposes, so instead of /create
you want Spanish /crear
, you can configure it by using Route::resourceVerbs()
method in App\Providers\RouteServiceProvider
:
public function boot(){ Route::resourceVerbs([ 'create' => 'crear', 'edit' => 'editar', ]); // ...}
When using Resource Controllers, in routes/web.php
you can specify ->names()
parameter, so the URL prefix in the browser and the route name prefix you use all over Laravel project may be different.
Route::resource('p', ProductController::class)->names('products');
So this code above will generate URLs like /p
, /p/{id}
, /p/{id}/edit
, etc.But you would call them in the code by route('products.index')
, route('products.create')
, etc.
Have you ever run "php artisan route:list" and then realized that the list takes too much space and hard to read?
Here's the solution:php artisan route:list --compact
Then it shows 3 columns instead of 6 columns: shows only Method / URI / Action.
+----------+---------------------------------+-------------------------------------------------------------------------+| Method | URI | Action |+----------+---------------------------------+-------------------------------------------------------------------------+| GET|HEAD | / | Closure || GET|HEAD | api/user | Closure || POST | confirm-password | App\Http\Controllers\Auth\ConfirmablePasswordController@store || GET|HEAD | confirm-password | App\Http\Controllers\Auth\ConfirmablePasswordController@show || GET|HEAD | dashboard | Closure || POST | email/verification-notification | App\Http\Controllers\Auth\EmailVerificationNotificationController@store || POST | forgot-password | App\Http\Controllers\Auth\PasswordResetLinkController@store || GET|HEAD | forgot-password | App\Http\Controllers\Auth\PasswordResetLinkController@create || POST | login | App\Http\Controllers\Auth\AuthenticatedSessionController@store || GET|HEAD | login | App\Http\Controllers\Auth\AuthenticatedSessionController@create || POST | logout | App\Http\Controllers\Auth\AuthenticatedSessionController@destroy || POST | register | App\Http\Controllers\Auth\RegisteredUserController@store || GET|HEAD | register | App\Http\Controllers\Auth\RegisteredUserController@create || POST | reset-password | App\Http\Controllers\Auth\NewPasswordController@store || GET|HEAD | reset-password/{token} | App\Http\Controllers\Auth\NewPasswordController@create || GET|HEAD | verify-email | App\Http\Controllers\Auth\EmailVerificationPromptController@__invoke || GET|HEAD | verify-email/{id}/{hash} | App\Http\Controllers\Auth\VerifyEmailController@__invoke |+----------+---------------------------------+-------------------------------------------------------------------------+
You can also specify the exact columns you want:
php artisan route:list --columns=Method,URI,Name
+----------+---------------------------------+---------------------+| Method | URI | Name |+----------+---------------------------------+---------------------+| GET|HEAD | / | || GET|HEAD | api/user | || POST | confirm-password | || GET|HEAD | confirm-password | password.confirm || GET|HEAD | dashboard | dashboard || POST | email/verification-notification | verification.send || POST | forgot-password | password.email || GET|HEAD | forgot-password | password.request || POST | login | || GET|HEAD | login | login || POST | logout | logout || POST | register | || GET|HEAD | register | register || POST | reset-password | password.update || GET|HEAD | reset-password/{token} | password.reset || GET|HEAD | verify-email | verification.notice || GET|HEAD | verify-email/{id}/{hash} | verification.verify |+----------+---------------------------------+---------------------+
If you use Route Model Binding and think you can't use Eager Loading for relationships, think again.So you use Route Model Binding
public function show(Product $product) { //}
But you have a belongsTo relationship, and cannot use $product->with('category') eager loading?You actually can! Load the relationship with ->load()
public function show(Product $product) { $product->load('category'); //}
If you use resource controllers, but want to change URL verbs to non-English, so instead of /create
you want Spanish /crear
, you can configure it with Route::resourceVerbs()
method.
public function boot(){ Route::resourceVerbs([ 'create' => 'crear', 'edit' => 'editar', ]); //}
In Resource Controllers, in routes/web.php
you can specify ->names()
parameter, so the URL prefix and the route name prefix may be different.This will generate URLs like /p
, /p/{id}
, /p/{id}/edit
etc. But you would call them:
Route::resource('p', \App\Http\Controllers\ProductController::class)->names('products');
Use Route::is('route-name')
to easily highlight your navbar menus
<ul> <li @if(Route::is('home')) class="active" @endif> <a href="/">Home</a> </li> <li @if(Route::is('contact-us')) class="active" @endif> <a href="/contact-us">Contact us</a> </li></ul>
Tip given by @anwar_nairi
route('page.show', $page->id);// http://laravel.test/pages/1route('page.show', $page->id, false);// /pages/1
Tip given by @oliverds_
You can override the route binding resolver for each of your models. In this example, I have no control over the @ sign in the URL, so using the resolveRouteBinding
method, I'm able to remove the @ sign and resolve the model.
// RouteRoute::get('{product:slug}', Controller::class);// Requesthttps://nodejs.pub/@unlock/hello-world// Product Modelpublic function resolveRouteBinding($value, $field = null){ $value = str_replace('@', '', $value); return parent::resolveRouteBinding($value, $field);}
Tip given by @Philo01
If you need public URL but you want them to be secured, use Laravel signed URL
class AccountController extends Controller{ public function destroy(Request $request) { $confirmDeleteUrl = URL::signedRoute('confirm-destroy', [ $user => $request->user() ]); // Send link by email... } public function confirmDestroy(Request $request, User $user) { if (! $request->hasValidSignature()) { abort(403); } // User confirmed by clikcing on the email $user->delete(); return redirect()->route('home'); }}
Tip given by @anwar_nairi
You can use the gates you specified in App\Providers\AuthServiceProvider
in middleware method.
To do this, you just need to put inside the can:
and the names of the necessary gates.
Route::put('/post/{post}', function (Post $post) { // The current user may update the post...})->middleware('can:update,post');
You can use php arrow function in routing, without having to use anonymous function.
To do this, you can use fn() =>
, it looks easier.
// Instead ofRoute::get('/example', function () { return User::all();});// You canRoute::get('/example', fn () => User::all());
You can use Route::view($uri , $bladePage)
to return a view directly, without having to use controller function.
//this will return home.blade.php viewRoute::view('/home', 'home');
You can create a /routes/web/ directory and only fill /routes/web.php with:
foreach(glob(dirname(__FILE__).'/web/*', GLOB_NOSORT) as $route_file){ include $route_file;}
Now every file inside /routes/web/ act as a web router file and you can organize your routes into diferent files.
If your routes have a lot of resource controllers, you can group them and call one Route::resources() instead of many single Route::resource() statements.
Route::resources([ 'photos' => PhotoController::class, 'posts' => PostController::class,]);
Did you know you can define custom route bindings in Laravel?
In this example, I need to resolve a portfolio by slug. But the slug is not unique, because multiple users can have a portfolio named 'Foo'
So I define how Laravel should resolve them from a route parameter
class RouteServiceProvider extends ServiceProvider{ public const HOME = '/dashboard'; public function boot() { Route::bind('portfolio', function (string $slug) { return Portfolio::query() ->whereBelongsto(request()->user()) ->whereSlug($slug) ->firstOrFail(); }); }}
Route::get('portfolios/{portfolio}', function (Portfolio $portfolio) { /* * The $portfolio will be the result of the query defined in the RouteServiceProvider */})
Tip given by @mmartin_joo
Here are two ways to check the route name in Laravel.
// #1<a href="{{ route('home') }}" @class="['navbar-link', 'active' => Route::current()->getName() === 'home']"> Home</a>// #2<a href="{{ route('home') }}" @class="['navbar-link', 'active' => request()->RouteIs('home)]"> Home</a>
Tip given by @AndrewSavetchuk
By default, when using route model binding will not retrieve models that have been soft-deleted.You can change that behavior by using withTrashed
in your route.
Route::get('/posts/{post}', function (Post $post) { return $post;})->withTrashed();
Tip given by @cosmeescobedo
If for some reason, your URL is having query parameters, you can retrieve the URL without query parameters using the fullUrlWithoutQuery
method of request like so.
// Original URL: https://www.amitmerchant.com?search=laravel&lang=en&sort=desc$urlWithQUeryString = $request->fullUrlWithoutQuery([ 'lang', 'sort']);echo $urlWithQUeryString;// Outputs: https://www.amitmerchant.com?search=laravel
Tip given by @amit_merchant
By default, Laravel throws a 404 error when it can't bind the model, but you can change that behavior by passing a closure to the missing method.
Route::get('/users/{user}', [UsersController::class, 'show']) ->missing(function ($parameters) { return Redirect::route('users.index'); });
Tip given by @cosmeescobedo
You can exclude middleware at the route level in Laravel using the withoutMiddleware method.
Route::post('/some/route', SomeController::class) ->withoutMiddleware([VerifyCsrfToken::class]);
Tip given by @alexjgarrett
Instead of using the controller in each route, consider using a route controller group. Added to Laravel since v8.80
// BeforeRoute::get('users', [UserController::class, 'index']);Route::post('users', [UserController::class, 'store']);Route::get('users/{user}', [UserController::class, 'show']);Route::get('users/{user}/ban', [UserController::class, 'ban']);// AfterRoute::controller(UsersController::class)->group(function () { Route::get('users', 'index'); Route::post('users', 'store'); Route::get('users/{user}', 'show'); Route::get('users/{user}/ban', 'ban');});
Tip given by @justsanjit