Laravel namespacing routes
Solution 1
You can use the namespace in the routes for that purpose :
Route::namespace('Landing\Controller')->group(function () {
Route::get('/', 'LandingController@index');
// + other routes in the same namespace
});
And dont forget to add the namespace to the controllers :
<?php namespace App\Lib\MyApp\Landing\Controller;
PS : in the case where the Lib is inside the App folder there is no need to add a thing in the composer file, because the App folder is registred in the psr-4
and with this it will load all the files within this namespase for you.
Solution 2
There are many ways to add the namespace in Laravel
Route::group(['prefix' => 'prefix','namespace'=>'Admin'], function () {
// your routes with"App\Http\Controllers\Admin" Namespace
});
Route::namespace('Admin')->group(function () {
// your routes with"App\Http\Controllers\Admin" Namespace
});
//single route
Route::namespace('Admin')->get('/todo', 'TaskController@index');
//single route
Route::get('/todo', 'Admin/TaskController@index');
// by ->namespace
Route::prefix('admin')->namespace('Admin')->group(function () {
// route code
});
Related videos on Youtube
Comments
-
Norgul almost 2 years
I have controllers in different folder than Laravel native
App\Http\Controllers
. I am using a customLib\MyApp
folder which has modules inside. Each module has its own controllers, models etc. I added tocomposer.json
autoloading toapp\lib
.What I did is change
RouteServiceProvider
namespace:protected $namespace = 'App\Lib\MyApp';
I did a
composer dump-autoload
after everything.Inside
MyApp
is aLanding\Controller
folder with actual controller class inside.Try 1 (ideal):
I would like to call my route like this:
Route::get('/', 'Landing\Controller\LandingController@index');
But this way I am getting a
ReflectionException
that the class is not found even thoughTry 2:
Route::get('/', '\Landing\Controller\LandingController@index');
Trailing slash gets rid of the namespace part when I refresh the page, and class is still said not to exist.
Try 3:
Route::get('/', 'MyApp\Landing\Controller\LandingController@index');
This just duplicates
MyApp
folder, and class is not found as expected.Try 4 (working, but don't want it like that)
Route::get('/', '\MyApp\Landing\Controller\LandingController@index');
This works fine, although I would like to get rid of the
\MyApp\
part.Is something like this possible?
-
Maraboc over 6 yearsWhat about namespace like this :
Route::namespace('Landing\Controller')->group(function () { Route::get('/', 'LandingController@index'); // + other routes in the same namespace });
?? -
Norgul over 6 yearsAlthough I like the approach, this one doesn't work with any combination
-
Maraboc over 6 yearsI think it must work according to the documentation :p
-
Norgul over 6 yearsYeah, but I have no idea what am I doing wrong...probably some wrong slash or something. The funny thing is that it is giving me back an error that class doesn't exist, even though the path it throws out is correct...
-
Maraboc over 6 yearsWaht did you add to the composer file ?? and is Lib insidethe App folder ?
-
Norgul over 6 yearsI wrote up there what is in composer. Yeah, lib is inside app. I thought maybe capitalization problem because lib is actually lowercase...but no avail
-
Maraboc over 6 yearsOne more thing did you namecpaced the
LandingController
like this<?php namespace App\Lib\MyApp\Landing\Controller;
?? -
Norgul over 6 yearsBOOM! Right on the spot :)
-