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?
Try getting help on the command
php artisan help make:controller
If you see a --resource
flag in the help options you are probably on 5.2 or newer and can add that flag to the command to get a resource controller.
php artisan make:controller --resource SomeResourceController
For Laravel 5.0 and 5.1 the make:controller
command would make a resource controller by default and the --plain
option would make a plain controller.
Laravel 5.2 – Restful Resource Controllers – Default plain
Laravel 5.1 – Restful Resource Controllers – Default resource
Laravel 5.0 – Restful Resource Controllers – Default resource
Summary: from Laravel 5.2 onward the make:controller
artisan command will create a plain controller by default.
Answer:
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.
Answer:
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
Answer:
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
Answer:
php artisan make:controller ControllerName --resource
Answer:
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.
Tags: laravel, php, phplaravel, rest