Laravel 9 Resource Controller And Route With Example
In this article, we'll delve into the implementation of Laravel 9's resource controller, focusing on the resource routes and how to utilize resource controllers effectively within a Laravel 9 application. By following this tutorial, you'll gain a solid understanding of resource routes and controllers in Laravel 9.
Laravel's resource controller and resource route constitute an intriguing feature that expedites the creation of CRUD applications in Laravel. The process involves two key steps: firstly, establishing a resource route that automatically generates routes for insertion, updating, viewing, and deleting; secondly, crafting a resource controller that houses the methods corresponding to these actions.
Let's now explore the rationale behind opting for resource routes over conventional routes. Traditionally, for CRUD applications, separate routes are defined as illustrated below:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ItemController;
Route::controller(ItemController::class)->group(function(){
Route::get('items', 'index')->name('items.index');
Route::post('items', 'store')->name('items.store');
Route::get('items/create', 'create')->name('items.create');
Route::get('items/{item}', 'show')->name('items.show');
Route::put('items/{item}', 'update')->name('items.update');
Route::delete('items/{item}', 'destroy')->name('items.destroy');
Route::get('items/{item}/edit', 'edit')->name('items.edit');
});
However, the same functionality can be achieved using resource routes, leading to a more concise and streamlined approach:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ItemController;
Route::resource('items', ItemController::class);
Once these resource routes are established, you can verify their creation by running the following command:
php artisan route:list
This command will provide a list of created routes.
Next, let's focus on generating the resource controller. Use the following command to create an ItemController equipped with resource methods:
php artisan make:controller ItemController --resource --model=Item
The generated ItemController will include methods such as `index`, `create`, `store`, `show`, `edit`, `update`, and `destroy`. These methods align with the CRUD operations and provide a foundation for your project's CRUD module.
Here's what the resulting ItemController might look like:
<?php
namespace App\Http\Controllers;
use App\Models\Item;
use Illuminate\Http\Request;
class ItemController extends Controller
{
public function index()
{
// Logic for displaying a list of items
}
public function create()
{
// Logic for displaying the create form
}
public function store(Request $request)
{
// Logic for storing a new item
}
public function show(Item $item)
{
// Logic for displaying a specific item
}
public function edit(Item $item)
{
// Logic for displaying the edit form
}
public function update(Request $request, Item $item)
{
// Logic for updating an existing item
}
public function destroy(Item $item)
{
// Logic for deleting an item
}
}
By embracing resource routes and controllers, you can effortlessly establish a robust CRUD module for your Laravel 9 project. This approach significantly reduces the complexity of managing separate routes for each CRUD operation, allowing you to focus on developing the core functionality of your application.
I trust that this guide has proven helpful to you in understanding and implementing resource controllers and routes within Laravel 9. If you have any further questions or concerns, feel free to reach out. Happy coding!
Leave A Comment