Laravel works on MVC(Model-View-Controller) architectural patterns. If you are running an application on the Laravel framework, then you have to create a model, view, and controllers for your application.
In Laravel, one can create a Controller and Model through the command line interface. Laravel artisan is the command-line interface that provides a number of commands which help us in building the application. We can and should create a Controller using the artisan command.
Create a Laravel Controller
Let’s say you are working on the back-end and you want to perform CRUD operations on pages. That means you need to build a system to manage pages which includes create, retrieve, update and delete pages. Of course, for this task, you need to create a controller.
Normally, people run the command php artisan make:controller PageController
. This command generates a file PageController.php
under the app/Http/Controllers directory.
Note: You need to run this command in the root directory of a project using the command-line interface.
PageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PageController extends Controller
{
//
}
In this generated controller, you need to write methods and define the routes accordingly. For each method, you have to write a separate route.
For instance. let’s add one method index()
and define a route for it.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PageController extends Controller
{
public function index() {
//do operations here
}
}
To call this index()
function in the route file we should add the below code.
routes/web.php
Route::get('pages', 'PageController@index');
The Same process will apply to each additional method.
There is nothing wrong with this flow. But this is not recommended way. Behind the popularity of Laravel, there are several reasons. One of the reasons is their powerful advanced artisan commands.
Better Way Of Creating Laravel Controller
In Laravel, we don’t need to write each method and define a separate route for each method. Using the artisan command we can generate a controller with pre-defined methods. And just by writing a single route Laravel automatically calls the appropriate methods.
Having said that, open your command line interface in the root directory of your project and run the below command. Keep a note, this time we are adding –resource to our make:controller command.
php artisan make:controller PageController --resource
Now the generated PageController.php
looks like as below.
PageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PageController 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 int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
We got the methods like index, create, store, show, edit, update, and destroy automatically. You can read the comments provided for each method which explains what each method does. Now let’s specify the route for it.
routes/web.php
Route::resource('pages', 'PageController');
This resourceful route will create multiple routes to handle different actions. Now, to get the details about the actions, run the command php artisan route:list
. For the pages route, you will see the output as shown in the screenshot.
This output gives you an idea of the HTTP method, URL, and which method will call on the specified URL.
For example, if you are running a URL YOUR_SITE_DOMAIN/pages with the GET method then it will give a call to the index()
method of a PageController.
If you want to create a new page the URL will be YOUR_SITE_DOMAIN/pages/create. It gives a call to the create()
method where you need to load a view for creating a new page.
When you submit a form with method=”post” and action=”pages”, it gives a call to the store()
method.
And so on.
It’s all about creating a Laravel controller for CRUD operations. Please share your thoughts in the comment section below.
Related Articles
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.