Search code examples
phplaravel-5ioc-container

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

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