December 7, 2016 —John Koster
The make:controller
command can be used to quickly generate a new controller class file in the app/Http/Controllers
directory. A name
for the newly created controller must be provided. The name
will become the name of the newly generated class and file. An optional --resource
option can be provided to generate a resourceful controller instead.
This command will not overwrite any existing controller classes that have the same name. An error stating Controller already exists!
will be displayed instead.
The following example would create a new controller file in the app/Http/Controllers/GeneratedController.php
file:
1php artisan make:controller GeneratedController
The generated controller file would look similar to the following:
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6 7use App\Http\Requests; 8 9class GeneratedController extends Controller10{11 //12}
This command would generate a new resource controller file in the app/Htpp/Controllers/GeneratedResourceController.php
file:
1php artisan make:controller GeneratedResourceController --resource
A resource controller has index
, create
, store
, show
, edit
, update
and destroy
method definitions already declared for you.
The make:controller
command is also capable of generated namespaced classes. Accomplish this by separating namespace sections using the \
when supplying the controller name. A nested directory will be created and the correct namespace will be set on the generated class.
∎
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.