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
?
When setting
namespace
inRoute::group()
it is actually appending that toApp\Http\Controllers
. What you could do is remove it and reference the full path like so: