Laravel Controller Subfolder routing

Tino picture Tino · Sep 17, 2013 · Viewed 120.6k times · Source

I'm new to Laravel. To try and keep my app organized I would like to put my controllers into subfolders of the controller folder.

controllers\
---- folder1
---- folder2

I tried to route to a controller, but laravel doesn't find it.

Route::get('/product/dashboard', 'folder1.MakeDashboardController@showDashboard');

What am I doing wrong?

Answer

Ja22 picture Ja22 · Apr 28, 2017

For Laravel 5.3 above:

php artisan make:controller test/TestController

This will create the test folder if it does not exist, then creates TestController inside.

TestController will look like this:

<?php
namespace App\Http\Controllers\test;

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

class TestController extends Controller
{
    public function getTest()
    {
        return "Yes";
    }
}

You can then register your route this way:

Route::get('/test','test\TestController@getTest');