Norgul
Norgul

Reputation: 4783

Laravel namespacing routes

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?

Upvotes: 4

Views: 21021

Answers (3)

Sehrish Waheed
Sehrish Waheed

Reputation: 1555

For "laravel 8" Here I have given an example with both namespace and prefix but you can also use any one according to your requirement.

I created Controller in Controllers dir with command

php artisan make:controller Admin/StoriesController

Route::namespace('Admin')->prefix('admin')->group(function(){
    Route::get('/deleted_stories', 
    '\App\Http\Controllers\Admin\StoriesController@index')->name
    ('admin.stories.index');
});

Upvotes: 0

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

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
});

Upvotes: 4

Maraboc
Maraboc

Reputation: 11083

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.

Upvotes: 6

Related Questions