Laravel namespacing routes

13,013

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
});
Share:
13,013

Related videos on Youtube

Norgul
Author by

Norgul

I'm cool

Updated on June 04, 2022

Comments

  • Norgul
    Norgul almost 2 years

    I have controllers in different folder than Laravel native App\Http\Controllers. I am using a custom Lib\MyApp folder which has modules inside. Each module has its own controllers, models etc. I added to composer.json autoloading to app\lib.

    What I did is change RouteServiceProvider namespace:

    protected $namespace = 'App\Lib\MyApp';
    

    I did a composer dump-autoload after everything.

    Inside MyApp is a Landing\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 though

    Try 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
      Maraboc over 6 years
      What about namespace like this : Route::namespace('Landing\Controller')->group(function () { Route::get('/', 'LandingController@index'); // + other routes in the same namespace }); ??
    • Norgul
      Norgul over 6 years
      Although I like the approach, this one doesn't work with any combination
    • Maraboc
      Maraboc over 6 years
      I think it must work according to the documentation :p
    • Norgul
      Norgul over 6 years
      Yeah, 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
      Maraboc over 6 years
      Waht did you add to the composer file ?? and is Lib insidethe App folder ?
    • Norgul
      Norgul over 6 years
      I 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
      Maraboc over 6 years
      One more thing did you namecpaced the LandingController like this <?php namespace App\Lib\MyApp\Landing\Controller; ??
    • Norgul
      Norgul over 6 years
      BOOM! Right on the spot :)