Laravel Routing Explained with Examples

In this post, we’ll walk you through how to define routes in Laravel 12, Laravel route examples for beginners, Laravel routing with clear examples, best practices, and tips to help you build clean and maintainable routes in your Laravel applications.

Laravel Routing Explained with Examples Image

Laravel, the popular PHP framework, is known for its elegant and powerful routing system. Whether you're building a small app or a large-scale application, Laravel’s routing makes it easy to map URLs to controllers, views, or closures.

Learn Laravel routing with simple examples. Understand route types, parameters, middleware, and best practices for building clean, scalable Laravel apps.

What is Routing in Laravel?

Routing in Laravel is the process of defining how your application responds to different URLs. It tells the framework what to do when a user accesses a specific endpoint in the browser.

 All Laravel routes are defined in the routes/web.php file for web-based routes and routes/api.php for API routes.

Basic Routing Example

use Illuminate\Support\Facades\Route;

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

 In this example, a GET request to the root URL (/) will return the welcome view.

Common HTTP Methods:

  • Route::get() – for retrieving data

  • Route::post() – for submitting data

  • Route::put() – for updating data

  • Route::delete() – for deleting data

Route Parameters

  Laravel allows you to capture URL segments using parameters.

  • Required Parameters
Route::get('/user/{id}', function ($id) {
   return "User ID is " . $id;
});
  • Optional Parameters
Route::get('/user/{name?}', function ($name = 'Guest') {
   return "Hello, " . $name;
});

Named Routes

 Named routes allow you to refer to routes when generating URLs or redirects.

Route::get('/dashboard', function () {
   return view('dashboard');
})->name('dashboard');

 You can generate a URL using the route name:

$url = route('dashboard');

Routing to Controllers

 Instead of closures, you can use controller methods for cleaner route handling.

  • Single Action Route
Route::get('/about', [PageController::class, 'about']);
  • Resource Controller

 If you're building a CRUD interface, Laravel’s resource() method saves time:

Route::resource('posts', PostController::class);

This single line creates multiple routes (index, create, store, show, edit, update, destroy).

Route Middleware

 You can protect routes using middleware like auth, admin, etc.

Route::get('/dashboard', function () {
   return view('dashboard');
})->middleware('auth');

 Group routes with middleware:

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

API Routes

 API routes are typically defined in routes/api.php and are stateless.

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
   return $request->user();
});

View Routes

If your route only needs to return a view, you may use the Route::view method. Like the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:

Route::view('/welcome', 'welcome');2 3Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

When using route parameters in view routes, the following parameters are reserved by Laravel and cannot be used: view, data, status, and headers.

Redirect Routes

If you are defining a route that redirects to another URI, you may use the Route::redirect method. This method provides a convenient shortcut so that you do not have to define a full route or controller for performing a simple redirect:

Route::redirect('/here', '/there');

By default, Route::redirect returns a 302 status code. You may customize the status code using the optional third parameter:

Route::redirect('/here', '/there', 301);

Or, you may use the Route::permanentRedirect method to return a 301 status code:

Route::permanentRedirect('/here', '/there');

When using route parameters in redirect routes, the following parameters are reserved by Laravel and cannot be used: destination and status.

Route Caching

 For performance, Laravel allows you to cache your routes in production:

php artisan route:cache

 To clear the route cache:

php artisan route:clear

Best Practices for Laravel Routing

  • Use Named Routes: Easier to reference and refactor.

  • Group Routes Logically: Use route groups and middleware wisely.

  • Keep Routes File Clean: Move logic to controllers.

  • Follow RESTful Conventions: Especially for APIs and CRUD systems.

Conclusion

You can check the official documentation for route in laravel's Routing page,

Laravel’s routing system is powerful, flexible, and easy to use. Whether you're handling simple page routes or building complex APIs, Laravel has the tools to make  your job easier.

 Now that you understand Laravel routing with examples, try applying it in your next project. Happy coding!

Tags

Do you Like?