Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gustav's avatar

Create Controller Inside a Subfolder

How do i create a controller using the Artisan make command inside a folder in the Controller folder ie app\Http\Controller\Mobile.

0 likes
17 replies
RachidLaasri's avatar

Just tried, and it works perfectly

php artisan make:controller frontend/SomeController

frontend is the name of your folder.

20 likes
bashy's avatar

Isn't it

php artisan make:controller Mobile/MobileController
6 likes
gustav's avatar

thanks bashy and rashid I have been trying with a backslash \ instead of /

martinbean's avatar

@gustav Quote the namespace, i.e.

php artisan make:controller "Mobile\SomeController"
4 likes
sebdesign's avatar

Just escape the backslash:

php artisan make:controller Mobile\\SomeController
4 likes
pranshu's avatar

I used the following on route.

Route::get('login', 'DashboardController@index');

also i check it create the admin folder. i try the group after creating the controller but it does not work for me

Route::group(['prefix' => 'adminPanel'], function () {
        Route::get('login', 'DashboardController@index');
    });

i also try

 composer dump-autoload

whats is wrong with my code i used php artisan to crate controller even by command

 php artisan make:controller adminPanel/DashboardController

i am using laravel 5.1 i also tried

php artisan route:scan

it shows no command found.

absiddiqueLive's avatar

@gustav You may try this it will work

php artisan make:controller subDriectory/YourController
php artisan make:controller subDriectory/YourController --plain
1 like
jekinney's avatar

You can effectively put controllers where ever you want, even not in http folder. Just ensure proper namespace is used in the routes file.

Your above snippet has the wrong namespace if your controller in in a subfolder.

1 like
mikimaine's avatar

if you are using Windows

   php artisan make:controller "Mobile\SomeController"

if Ubuntu

   php artisan make:controller "Mobile/SomeController"

1 like
mbengsamba's avatar

Try php artisan make:controller Mobile/Your_Controller_Name

Shubham's avatar

Use php artisan:make controller Mobile\\SampleController

Then inside the controller make sure to include

use App\Http\Controllers\Controller

oooALIooo's avatar

@bobbybouwmann but it's possible by this command ( on windows ) :

php artisan make:controller SubFolder/ControllerName

it worked for me on laravel 5.4 .

bobbybouwmann's avatar

I know...

It was first not possible.. Learned something today ;)

2 likes
AdiInviter's avatar

To create a controller in subfolder :

php artisan make:controller "Test/SampleController"

Then inside SampleController.php :

<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class SampleController extends Controller
{
    public function index()
    {
        return 'Its working!';
    }
}

In routes/Web.php :

Route::get('something', 'Test\SampleControllerextends@index');

NOTE

Do not use forward slash in your routes/Web.php when defining a route :

// WRONG :
Test/SampleControllerextends@index

Instead use a back slash like :

// Correct :
Test\SampleControllerextends@index

Another Technique :

Create your controllers in a subfolder like :

Http
|
|-Controllers
   |
   |-Test
      |
      |-FirstController.php
      |-SecondController.php
      |-ThirdController.php

Now in your routes/Web.php, do not specify folder :

Route::get('/first', 'FirstController@index');
Route::get('/second', 'SecondController@index');
Route::get('/third', 'ThirdController@index');

Then run :

composer dumpautoload

And it should work!

Cheers..

2 likes
Ravaelles's avatar

In Laravel 8 it's possible to do it like this:

Route::get(
     'panel/{panel?}',
     [PanelController::class, 'index']
)->name('panel.index');

My PanelController resides outside App\Controllers namespace.

Please or to participate in this conversation.