Laravel 9 resource route, controller example; In this tutorial, we will learn how to create a resource route, controller, API resource route, and API resource controller using command line or manually in laravel 9 app.
Laravel 9 Resource Controller And Routes Example Tutorial
Let’s see the following stesp to create and use resource route, controller with modal in laravel 9 apps:
- Controller Using Artisan Command
- Create a Simple Controller
- Create a Resource Controller
- Create a Resource Controller with Model
- Routes
- Create Simple Routes
- Create Resource Routes
- API Controller and Routes
Controller Using Artisan Command
Now, we will show you how to create simple and resource controller in laravel 9 app using artisan command.
So open your terminal and navigate to your laravel 9 app directory. After that, use the below-given command to create simple and resource controller in laravel 9 app.
Create a Simple Controller
To create simple controller in laravel 9 app by the following command:
php artisan make:controller BOOKController
The above command will create a simple controller file inside app/http/controllers directory. When you open it, you will look like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BOOKController extends Controller
{
}
Create a Resource Controller
To create a Resource controller in laravel 9 app by the following command:
php artisan make:controller CRUDController --resource
The above command will create a resource controller file inside app/http/controllers directory. When you open it, you will look like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BOOKController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
}
/**
* Display the specified resource.
*
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function show(Book $book)
{
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function edit(Book $book)
{
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Book $book)
{
}
/**
* Remove the specified resource from storage.
*
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function destroy(Book $book)
{
}
}
Note that, You saw above 2 commands to create simple controller and resource controller. Also saw that there is a slight difference in the command to create both controllers. By running the command of simple controller, a simple controller file is created. But when you execute the command to create the resource controller. Then resource controller contains bydefault index (), create (), edit (), update (), destroy () method inside it.
Create a Resource Controller with Model
To create Resource controller in laravel 9 app by the following command:
php artisan make:controller BOOKController --resource --model=book
The above command will create resource controller with model file. And controller file has located inside app/http/controllers directory. And Model file has been located inside app/Models directory.
If you open controller file, you will look like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
Use App\Models\Book;
class BOOKController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
}
/**
* Display the specified resource.
*
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function show(Book $book)
{
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function edit(Book $book)
{
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Book $book)
{
}
/**
* Remove the specified resource from storage.
*
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function destroy(Book $book)
{
}
}
If you open Model file, you will look like:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Crud extends Model
{
use HasFactory;
}
Routes
Now, we will show you how to define or create simple and resource rotues in laravel 9 app.
So, Navigate to your laravel 9 app directory. And open web.php file, which is placed inside routes directory.
Create Simple Routes
Create Simple routes for crud application in laravel 9 app:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BOOKController;
Route::get('books',['as'=>'books.index','uses'=>'BOOKController@index']);
Route::post('books/create',['as'=>'books.store','uses'=>'BOOKController@store']);
Route::get('books/edit/{id}',['as'=>'books.edit','uses'=>'BOOKController@edit']);
Route::patch('books/{id}',['as'=>'books.update','uses'=>'BOOKController@update']);
Route::delete('books/{id}',['as'=>'books.destroy','uses'=>'BOOKController@destroy']);
Route::get('books/{id}',['as'=>'books.view','uses'=>'BOOKController@view']);
Create Resource Routes
Create resource routes for crud application in laravel 9 app:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CRUDController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::resource('crud', CRUDController::class);
Then open terminal and run the following command on it:
php artisan route:list
The following command will display resource routes methods:
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+-----------+-------------------+---------------+---------------------------------------------+--------------+ | | GET|HEAD | api/user | | Closure | api,auth:api | | | GET|HEAD | books | books.index | App\Http\Controllers\BOOKController@index | web | | | POST | books | books.store | App\Http\Controllers\BOOKController@store | web | | | GET|HEAD | books/create | books.create | App\Http\Controllers\BOOKController@create | web | | | GET|HEAD | books/{book} | books.show | App\Http\Controllers\BOOKController@show | web | | | PUT|PATCH | books/{book} | books.update | App\Http\Controllers\BOOKController@update | web | | | DELETE | books/{book} | books.destroy | App\Http\Controllers\BOOKController@destroy | web | | | GET|HEAD | books/{book}/edit | books.edit | App\Http\Controllers\BOOKController@edit | web | +--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
Note that, If you use simple routes. Then you need to define all routes in routes file. But if you use resource route. Then you just have to write the resource in front of the single route. As you saw in the above-given example.
API Controller and Routes
To create resource controller by using the following command:
Create Resource Controller
To create simple controller in laravel 9 app by the following command:
php artisan make:controller API\BOOKController- resource
The above command will create a simple controller file inside app/http/controllers/API directory. When you open it, you will look like:
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BOOKController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
}
/**
* Display the specified resource.
*
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function show(Book $book)
{
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function edit(Book $book)
{
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Book $book)
{
}
/**
* Remove the specified resource from storage.
*
* @param \App\Book $book
* @return \Illuminate\Http\Response
*/
public function destroy(Book $book)
{
}
}
Define Resource API Route
Now, navigate to routes directory and open api.php. Then you can define resource api routes in laravel 9 app:
use App\Http\Controllers\API\BOOKController;
Route::resource('book', [BOOKController::class]);
Conclusion
In this laravel 9 resource route, controller example, you have learned how to create resource controller and api resource controller using artisan command in laravel 9 app. And how to define resource routes and api resource routes in laravel 9 app.
Recommended Laravel Tutorials