Skip to content

Sobhan-nz/laravel-routing-tutorial-v2.0-en

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

📌 Complete Tutorial on Routing in Laravel

1. Definition of Routing in Laravel

In Laravel, Route is responsible for determining what happens when a user requests a specific address (URL).

Routing is the bridge between the URL that the user requested and the application logic (Controller, View, Closure Function).

Example:

User → Browser → /about → Route → Controller or View → Response to user

2. Route definition location

Main route files in routes/ folder:

  • web.php → For web requests (with Session and CSRF)
  • api.php → For APIs (without Session)
  • console.php → Console routes (Artisan Commands)
  • channels.php → Channel routes for Broadcast

3. Types of Route Definition

3.1 Simple route with Closure

Route::get('/about', function () {
return 'About Us Page';
});

3.2 Route to a View

Route::get('/contact', function () {
return view('contact');
});

3.3 Route to a Controller

Route::get('/products', [ProductController::class, 'index']);

4. HTTP Methods in Route

  • GET → Get Data
  • POST → Send Data
  • PUT/PATCH → Update Data
  • DELETE → Delete Data
  • ANY → Any Method
  • MATCH → Multiple Specific Methods

Example:

Route::post('/form', [FormController::class, 'store']);
Route::delete('/product/{id}', [ProductController::class, 'destroy']);

5. Parameters in Route

5.1 Required

Route::get('/user/{id}', function ($id) {
return "User ID: $id";
});

5.2 Optional

Route::get('/user/{name?}', function ($name = 'Guest') {
return "Hello $name";
});

5.3 Validation with Regex

Route::get('/product/{id}', function ($id) {
return "Product ID $id";
})->where('id', '[0-9]+');

6. Route Naming

Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

In Blade: Blade dashboard


---

## 7. Route grouping
### 7.1 with Prefix
```php
Route::prefix('admin')->group(function () { 
Route::get('/users', [AdminController::class, 'users']); 
Route::get('/posts', [AdminController::class, 'posts']);
});

7.2 with Middleware

Route::middleware(['auth'])->group(function () { 
Route::get('/profile', [UserController::class, 'profile']); 
Route::get('/settings', [UserController::class, 'settings']);
});

8. Route Resource (Fast CRUD)

Route::resource('products', ProductController::class);

The following routes are created:

  • index
  • create
  • store
  • show
  • edit
  • update
  • destroy

9. Route Fallback (404 page)

Route::fallback(function () {
return view('errors.404');
});

10. Caching Routes

php artisan route:cache
php artisan route:clear

📚Conclusion

Routing is one of the most important parts of Laravel that handles requests and responses. By using Routes properly, you can create a clean, secure, and maintainable structure.

About

Version 2.0.0 – Comprehensive Laravel Routing Guide (English)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published