Laravel routes are used to bind url to controller action. When you create new pages in Laravel, you have to remember three things:
What is REST API routes?
Folllowings are some of the commonly used HTTP methods. These methods basically corresponds to create, read, update and delete operations.
Route::get($uri, $callback); // Used to list records from the database Route::post($uri, $callback); // Used to create a record in the database Route::put($uri, $callback); // Used to update or replace record in database Route::patch($uri, $callback); // Used to update or modify the records in database Route::delete($uri, $callback); // Used to delete a record from the table
How to define route in Laravel?
Routes in Laravel are defined in following location:
Followings are some of the example of basic routes:
# Defines a route that lists all the users using GET method Route::get('user', [UserController::class, 'show'])->name('user.show'); # Defines a route that creates user using POST method Route::post('user', [UserController::class, 'store'])->name('user.store'); # Defines a route that update user partially using PATCH method Route::patch('/user/:id', [UserController::class, 'update'])->name('user.update'); # Defines a route that deletes user using DELETE method Route::delete('/user/:id', [UserController::class, 'delete'])->name('user.delete');
How to define a route without controller?
# Creates a login route with extension and returns view Route::get('login', function() { return view('auth.login'); }); # Route that returns view directly using GET method Route::view('/dashboard', 'dashboard'); # Route post login Route::post('login', function() { // Your logic to login }); # Route that returns view by passing some variables to view Route::view('/welcome', 'welcome', ['name' => 'John Doe']);
How to define routes with parameters?
# Define a route where id should be numeric only Route::get('/user/{id}', [UserController::class, 'show'])->where('id', '[0-9]+'); # Define a route where name should be alpha only Route::get('/user/{name}', [UserController::class, 'getName'])->where('name', '[A-Za-z]+'); # Define a route where name should be alpha only and id should be numeric only Route::get('/user/{id}/{name}', [UserController::class, 'get'])->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
Routes with groups and middlewares
If you decided to group some routes that needs to have certain middlewares attached to them you can easily define them in Laravel. Let say that your application has both front and backend.
Only logged in users are allowed to access some urls. You can define a middleware that checks to see if user is logged on or not and then group some routes so that they implement such middleware.
Let's check the following examples:
Route::middleware(['login'])->group(function () { Route::get('user', [UserController::class, 'get']); Route::get('user/:id', [UserController::class, 'show']); }); Route::middleware(['web'])->group(function () { Route::get('login', [AuthController::class, 'login']); Route::get('logout', [AuthController::class, 'logout']); });
Using many middleware:
Route::middleware(['first', 'second'])->group(function () { Route::get('/', function () { // Uses first & second middleware... }); Route::get('/user/profile', function () { // Uses first & second middleware... }); });
Define routes with prefix
Route::prefix('admin')->group(function() { Route::get('user', [UserController::class, 'get']); Route::get('users/:id', [UserController::class, 'show']); });
How to get current route name or action or route?
# Get current route $route = Route::current(); # get current route name $name = Route::currentRouteName(); # get current route controller action $action = Route::currentRouteAction();
How to define 404 route?
# Define not found route Route::fallback([HomeController::class, 'notFound'])->name('notFound'); # Define 404 route without controller Route::fallback(function () { return view("404"); });
How to group routes based on sub-domain
Route::domain('{sub_domain}.example.com')->group(function () { Route::get('user/{id?}', function ($sub_domain, $id) { // Your logic handle }); });
How to define route name
Route::get('/user/profile', function () { // Your logic handle })->name('profile');
How to using a route name
// Redirect to a route name return redirect()->route('profile'); // Using in link <a href="{{ route('profile') }}">Go to profile</a>
Artisan commands for routing
Show list route of application:
php artisan route:list
Clear cache of route:
php artisan route:clear
Store route to cache:
php artisan route:cache