Routing in Laravel is one of the core functionalities that allows you to map URL patterns to specific actions in your application. Here’s a breakdown of the basics of using routes in Laravel:
Certainly! Let’s dive deeper into each aspect of routing in Laravel:
1. Defining Routes
Routes are defined in the routes/web.php
(for web interfaces) and routes/api.php
(for APIs).
Example:
Route::get('/welcome', function () {
return 'Welcome to Laravel!';
});
This defines a route that responds to a GET request at the /welcome
URL.
2. HTTP Methods
Laravel supports various HTTP methods: GET, POST, PUT, DELETE, PATCH, OPTIONS.
- GET Request:
Route::get('/get-example', function () {
return 'GET request to /get-example';
});
Used for fetching data.
- POST Request:
Route::post('/post-example', function () {
return 'POST request to /post-example';
});
Used for creating new resources.
- PUT Request:
Route::put('/put-example', function () {
return 'PUT request to /put-example';
});
Used for updating existing resources.
- DELETE Request:
Route::delete('/delete-example', function () {
return 'DELETE request to /delete-example';
});
Used for deleting resources.
- PATCH Request:
Route::patch('/patch-example', function () {
return 'PATCH request to /patch-example';
});
Used for partially updating resources.
3. Route Parameters
Parameters in routes allow you to capture values from the URL.
- Required Parameters:
Route::get('/user/{id}', function ($id) {
return 'User ID: '.$id;
});
Here, {id}
is a required parameter. Access it within the route’s callback function.
- Optional Parameters:
Route::get('/user/{name?}', function ($name = 'Guest') {
return 'User Name: '.$name;
});
The ?
makes the parameter optional, and Guest
is the default value if no name is provided.
4. Named Routes
Naming routes helps in generating URLs or redirects conveniently.
- Defining a Named Route:
Route::get('/user/profile', function () {
return 'User Profile';
})->name('profile');
- Generating URLs:
$url = route('profile');
- Generating Redirects:
return redirect()->route('profile');
5. Route Groups
Route groups allow you to apply attributes (like middleware, namespaces, prefixes) to multiple routes.
- Middleware:
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', function () {
// Auth middleware applied
});
Route::get('/account', function () {
// Auth middleware applied
});
});
Here, the auth
middleware is applied to all routes within the group.
- Namespaces:
Route::namespace('Admin')->group(function () {
Route::get('/users', 'UserController@index');
});
This assumes controllers are within the App\Http\Controllers\Admin
namespace.
- Prefixes:
Route::prefix('admin')->group(function () {
Route::get('/users', function () {
return 'Admin Users';
});
});
All routes within the group will be prefixed with admin
.
6. Route Controllers
For more complex logic, you can direct routes to controller methods instead of defining closures.
use App\Http\Controllers\UserController;
Route::get('/user/{id}', [UserController::class, 'show']);
In the UserController
, you should have a show
method:
public function show($id)
{
return 'User ID: '.$id;
}
7. Route Model Binding
Laravel can automatically inject model instances into your routes based on the route parameters.
use App\Models\User;
Route::get('/user/{user}', function (User $user) {
return $user;
});
Laravel automatically fetches the User
model that matches the provided {user}
parameter.
8. Resource Controllers
Resource controllers simplify the process of creating routes for a CRUD application by providing a single command to create multiple routes.
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
This creates the following routes:
GET /posts
– indexGET /posts/create
– createPOST /posts
– storeGET /posts/{post}
– showGET /posts/{post}/edit
– editPUT/PATCH /posts/{post}
– updateDELETE /posts/{post}
– destroy
Your PostController
would look something like this:
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return Post::all();
}
public function create()
{
// return view to create a post
}
public function store(Request $request)
{
// store new post logic
}
public function show(Post $post)
{
return $post;
}
public function edit(Post $post)
{
// return view to edit the post
}
public function update(Request $request, Post $post)
{
// update post logic
}
public function destroy(Post $post)
{
// delete post logic
}
}