Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 861
Vicky Avatar asked Jan 08 '16 12:01

Vicky


People also ask

How do you make a resource controller in Laravel?

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.

What is the command of creating a controller in Laravel?

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.

What is restful controllers in Laravel?

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 / ...

How do I run artisan command in Laravel controller?

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.


2 Answers

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. 
like image 38
Yogesh Yadav Avatar answered Sep 18 '22 07:09

Yogesh Yadav


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.

like image 132
lagbox Avatar answered Sep 21 '22 07:09

lagbox