'How to create a RESTful Resource Controller in Laravel 5.2, using Artisan command (PHP)
I'm working with Laravel 5 and I would like to know how to generate a RESTful Resource Controller with all predefined methods using the Artisan command (PHP).
When I run php artisan make:controller LessonsController, it creates a controller, with no methods as shown below:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class LessonsController extends Controller
{
}
What I want to create is a complete Laravel RESTful Resource Controller with all predefined methods as in: index(), create(), store(), show(), edit(), update() and destroy().
How can I achieve this?
Solution 1:[1]
For Laravel 5.2
php artisan make:controller NameofController --resource
// It will create the controller with all methods.
php artisan make:controller NameofController
// It will create the controller with all methods.
and
php artisan make:controller NameofController --plain
// It will create the controller without any method.
Solution 2:[2]
For default controller which have all methods you want,php artisan make:controller LessonsController
If you want plain controller with no method,php artisan make:controller --plain LessonsController
Solution 3:[3]
php artisan make:controller "NameOfController" - will create controller with all methods
php artisan make:controller "NameOfController" --plain This will create controller with no methods.
Best Regards, I am using laravel 5.0
Solution 4:[4]
php artisan make:controller ControllerName --resource
Solution 5:[5]
so you are using Laravel 5.2, so to have the controller with RESTful methods issue the command
php artisan make:controller --resource NAME_OF_CONTROLLER
In Laravel 5.1 and below, by default the make:controller command used to generate the Controller with all required methods such as 'index, create, store, show, edit, update, destroy'. And for 5.1 and below, to have the blank controller file without any methods, we used to use '--plain' parameter as
php artisan make:controller --plain NAME_OF_CONTROLLER
But with Laravel 5.2, by default the artisan command will create the bare controller file without any RESTful methods.
As Laravel 5.2 has many changes, it is better to use the 'artisan help' command as below
php artisan help make:controller
With this, we will realize the introduction of --resource
Please Refer the Laravel Documentation Laravel HTTP Controllers - Artisan Command
Suggestion: As this is the change from 5.2, it would be good to edit the Post Title too.
Solution 6:[6]
php artisan make:controller ItemController --resource
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yogesh Yadav |
| Solution 2 | Anisur Rahman |
| Solution 3 | |
| Solution 4 | David Arenburg |
| Solution 5 | |
| Solution 6 | Md.Mahbub Murshid |
