3

In my Laravel app I'm splitting the front end and back end code into folder. These are app/Http/Controllers/BackEnd and app/Http/Controllers/FrontEnd. Rather than type all that out on every file I thought it would be easier to define two namespaces, BackEnd and FrontEnd. I've edited my composer file to this:

"autoload": {
    "classmap": [
        "app/Models",
        "database"
    ],
    "psr-4": {
        "App\\": "app/",
        "BackEnd\\": "app/Http/Controllers/BackEnd",
        "FrontEnd\\": "app/Http/Controllers/FrontEnd"
    }
},

I then ran composer autodump and set up my route file like this:

Route::group(['prefix' => 'webman', 'middleware' => 'auth', 'namespace' => 'BackEnd'], function()
{
   Route::get('/', ['as' => 'webmanHome', 'uses' => 'HomeController@index']); 
});

But when I browse to localhost:8000/webman/ I just get an error, Class App\Http\Controllers\BackEnd\HomeController does not exist. The controller does exist, this is the file:

<?php namespace BackEnd;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HomeController extends Controller {

    /**
     * Display the admin home page, showing the front-end menu and "apps" as links to other sections of the ACP.
     *
     * @param Reqeust       $request
     *
     * @return View
     */
    public function index(Request $request)
    {
        return view('backend.index');
    }

}

I've checked vendor/composer/autoload_psr4.php to make sure that the namespace is being defined and it is, this is in the array returned 'BackEnd\\' => array($baseDir . '/app/Http/Controllers/BackEnd'),.

Strangely if I use <?php namespace App\Http\Controllers\BackEnd; at the top of HomeController.php then everything works, why? What am I missing? Why can't autoload find the file with just BackEnd?

1
  • maybe your blade template tries to access controller at wrong location. (if you are using 'action' refer) so specify controller in blade like BackEnd\HomeController@lala Commented Jun 29, 2015 at 9:52

3 Answers 3

5

When setting namespace in Route::group() it is actually appending that to App\Http\Controllers. What you could do is remove it and reference the full path like so:

Route::group(['prefix' => 'webman', 'middleware' => 'auth'], function()
{
    Route::get('/', ['as' => 'webmanHome', 'uses' => '\BackEnd\HomeController@index']); 
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this solves my issue. Can you please explain why using 'namespace' => '\BackEnd' doesn't work either?
The namespace value is getting trimmed of slashes and then appended to App\Http\Controllers. Take a look in the source, and specifically the formatUsesPrefix() method in vendor/laravel/framework/src/Illuminate/Routing/Router.php.
0

Try changing/commenting the below line in RouteServiceProvider.php protected $namespace = 'App\Http\Controllers';

1 Comment

It would probably be a good idea to include info about what this does and how this is likely to break other routes.
0

There's an interesting and easy way to get around this... Service Providers.

When the route file is loaded via a provider, the 'App\Http...' is not enforced.

public function boot()
{
    $this->loadRoutesFrom(app_path('Your/Model/routes.php'));
}

Keep in mind no middleware is attached either. Your route group will have to specify a 'web' middleware or you'll go nuts wondering why validation, etc, isn't working anymore....(been there!)

It's a cool way to go about it anyway, using providers leads to more modular code and re-use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.