Generating RESTful Controllers in Laravel 5.2
In Laravel 5.2, the standard php artisan make:controller command generates a blank class. This is by design; the framework philosophy assumes you want to define your own logic rather than inheriting boilerplate methods immediately. If you need a controller with the full suite of RESTful actionsâindex, create, store, show, edit, update, and destroyâyou cannot achieve this solely by running that specific command.
To generate a controller with these predefined methods, you must use the dedicated resource generation command: php artisan make:resource-controller. However, there is a critical version-specific nuance here. The syntax for generating a resource controller changed significantly between Laravel 5.1 and Laravel 5.2.
In Laravel 5.2, the Artisan command to create a resource controller with all standard methods (index, create, store, show, edit, update, destroy) is:
php artisan make:resource LessonsController --resource=Lesson
When you run this command, Laravel does not just create an empty class. Instead, it scaffolds a controller that extends Illuminate\Routing\Controller and includes the necessary traits for resource manipulation. The generated file will look something like this:
<?php
namespace App\Http\Controllers;
use App\Lesson;
use App\Http\Requests\LessonResourceRequest;
use Illuminate\Routing\Controller as BaseController;
class LessonsController extends BaseController
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return Lesson::all();
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
// ... and so on for store, show, edit, update, destroy
}
}
The command automatically infers the model name from the controller suffix (LessonsController maps to Lesson). It also attempts to generate a corresponding request validation class named LessonResourceRequest. This is a significant time-saver compared to manually writing stubs for every HTTP verb.
Understanding the Scaffolding
The generated controller uses Laravel's resource() method internally (implicitly via the structure) to define routes. While you haven't explicitly called $this->resource() yet, the controller methods are structured to align with the standard RESTful route definitions found in routes/web.php. You will typically pair this controller with a resource route definition like:
Route::resource('lessons', 'LessonsController');
This single line of code maps all seven HTTP verbs (GET, POST, PUT, DELETE) to their corresponding controller methods. For example, a GET request to /lessons triggers the index() method, while a POST request to /lessons triggers store().
Important Implementation Details
One common pitfall when using the generated resource controller is forgetting that it expects specific model relationships and validation rules. The scaffolding assumes your model exists and that the request class (LessonResourceRequest) is located in the standard namespace path (App\Http\Requests). If you have custom validation logic, ensure you copy the generated request file or override its methods appropriately.
Another consideration is the return type. The stubbed methods often use return Lesson::all(); for the index action and return view('lessons.show'); for the show action. While convenient, this couples your controller tightly to specific views or Eloquent queries. In a production environment, you might prefer injecting a repository or using an interface to decouple the controller from the data layer, adhering to dependency injection principles.
Version Compatibility Note
It is vital to distinguish this behavior from later versions of Laravel. In Laravel 5.3 and above, the command syntax shifted slightly, and the default stubs were updated in subsequent versions (like 5.4+). If you are strictly on Laravel 5.2 as indicated in your setup, ensure you do not mix commands intended for higher versions. The --resource flag is the key differentiator here; without it, you get a blank controller. With it, you get a fully functional scaffold ready for immediate development, provided you have the corresponding Model and Views set up.
By utilizing the correct Artisan command, you bypass the initial boilerplate work and jump straight into implementing custom business logic within the standardized RESTful methods that Laravel provides out of the box.