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?
Creating the Controller This is the easy part. From the command line in the root directory of your Laravel project, type: php artisan make:controller sharkController --resource This will create our resource controller with all the methods we need.
Step 1 − Create a controller called MyController by executing the following command. app/Http/Controllers/MyController. php file. Step 3 − Add the following line of code in app/Http/routes.
A restful controller follows the standard blueprint for a restful resource which mainly consists of: GET /resource index resource.index GET /resource/create create resource.create POST /resource store resource.store GET /resource/{resource} show resource.show GET /resource/{resource}/edit edit resource.edit PUT/PATCH / ...
So we can do it by using Artisan facade. In Laravel Artisan facade that way we can easily run the all artisan command also with argument. So Artisan facade have two method one call() and another one is queue() through we can simply make process in call like seeder and also migration run etc.
For Laravel 5.2
php artisan make:controller NameofController --resource // It will create the controller with all methods.
If Laravel < 5.2
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With