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
1) That is how you can make your app organized:
Every route file (web.php
, api.php
...) is declared in a map()
method, in a file
\app\Providers\RouteServiceProvider.php
When you mapping a route file you can set a ->namespace($this->namespace)
for it, you will see it there among examples.
It means that you can create more files to make your project more structured!
And set different namespaces for each of them.
But I prefer set empty string
for the namespace ""
2) You can set your controllers to rout in a native php way, see the example:
Route::resource('/users', UserController::class);
Route::get('/agents', [AgentController::class, 'list'])->name('agents.list');
Now you can double click your controller names in your IDE to get there quickly and conveniently.
I had this problem recently with laravel 5.8 but i underestand I should define controller in a right way like this below:
php artisan make:controller SubFolder\MyController // true
Not like this:
php artisan make:controller SubFolder/MyController // false
Then you can access the controller in routes/web.php like this:
Route::get('/my', 'SubFolder\MyController@index');
php artisan make:controller admin/CategoryController
Here admin is sub directory under app/Http/Controllers and CategoryController is controller you want to create inside directory
In my case I had a prefix that had to be added for each route in the group, otherwise response would be that the UserController class was not found.
Route::prefix('/user')->group(function() {
Route::post('/login', [UserController::class, 'login'])->prefix('/user');
Route::post('/register', [UserController::class, 'register'])->prefix('/user');
});
For those using Laravel 5 you need to set the namespace for the controller within the sub-directory (Laravel 5 is still in development and changes are happening daily)
To get a folder structure like:
Http
----Controllers
----Admin
PostsController.php
PostsController.php
namespace Admin\PostsController.php file like so:
<?php namespace App\Http\Controller\Admin;
use App\Http\Controllers\Controller;
class PostsController extends Controller {
//business logic here
}
Then your route for this is:
$router->get('/', 'Admin\PostsController@index');
And lastly, don't for get to do either composer or artisan dump
composer dump-autoload
or
php artisan dump
For ** Laravel 5 or Laravel 5.1 LTS both **, if you have multiple Controllers in Admin folder, Route::group
will be really helpful for you. For example:
Update: Works with Laravel 5.4
My folder Structure:
Http
----Controllers
----Api
----V1
PostsApiController.php
CommentsApiController.php
PostsController.php
PostAPIController:
<?php namespace App\Http\Controllers\Api\V1;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PostApiController extends Controller {
...
In My Route.php, I set namespace
group to Api\V1
and overall it looks like:
Route::group(
[
'namespace' => 'Api\V1',
'prefix' => 'v1',
], function(){
Route::get('posts', ['uses'=>'PostsApiController@index']);
Route::get('posts/{id}', ['uses'=>'PostssAPIController@show']);
});
For move details to create sub-folder visit this link.