Routing in Laravel defines how an application responds to incoming HTTP requests. It maps URLs to specific actions such as returning a view, executing a controller method, or processing data. Laravel’s routing system is simple, expressive, and powerful.
Laravel routes are defined inside the routes/ directory:
web.php – Routes for web applications (supports sessions, cookies, CSRF)
api.php – Routes for APIs (stateless, prefixed with /api)
console.php – Artisan command routes
channels.php – Broadcasting channels
Most applications primarily use web.php and api.php.
A basic route returns a response when a URL is accessed:
Here:
get is the HTTP method
/ is the URL
The function returns a response
Routes can directly return views:
The view file must exist in resources/views/about.blade.php.
Controllers keep logic organized:
This route calls the index method of UserController.
Routes can accept parameters:
Optional parameters:
Named routes make URL generation easier:
Usage:
Route groups help organize routes:
Resource routes automatically create CRUD routes:
This creates routes for:
index
create
store
show
edit
update
destroy
API routes are defined in api.php:
These routes are stateless and automatically prefixed with /api.
For better performance, Laravel allows route caching:
To clear cache:
Laravel routing provides a clean and flexible way to handle HTTP requests. With features like route parameters, named routes, middleware, and resource routing, Laravel makes building both web and API applications efficient and well-structured.
Take quizzes related to this topic and see where you stand!
Start Quiz Now