Oct 25, 2016

How to make controller inside a sub-directory in Laravel project

Assume we wanna create our API, so we create a namespace/sub-directory called API inside app/Http/Controllers (in order to distinguish with others sub-namespaces. The fact that, we can effectively put our controllers where ever we want, even not in Http directory).
For example:
php artisan make:controller API/AuthController
php artisan make:controller Admin/DashboardController
And then we define our routes like:
Route::group(['prefix' => 'api', 'namespace' => 'API'], function() {
    Route::post('login', 'AuthController@login');
});

Route::group(array('prefix' => 'adm', 'namespace' => 'AdminPanel'), function() {
    Route::get('/', 'DashboardController@index');
});
This is a simple way for us to have a nice Laravel structure.

No comments:

Post a Comment