我发现很多关于将控制器放在子文件夹中的帖子,大多数(如果不是全部)似乎都与命名空间或命名问题有关。我似乎无法让它工作,并继续得到以下ReflectionException和FatalError报告该类正在使用中。
[2016-12-22 16:51:13] local.ERROR: ReflectionException: Class App\Http\Controllers\Admin\DashboardController does not exist in /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Container/Container.php:749
Stack trace:
#0 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Container/Container.php(749): ReflectionClass->__construct('App\\Http\\Contro...')
#1 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Container/Container.php(644): Illuminate\Container\Container->build('App\\Http\\Contro...', Array)
#2 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(709): Illuminate\Container\Container->make('App\\Http\\Contro...', Array)
#3 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Foundation\Application->make('App\\Http\\Contro...')
#4 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Routing/Route.php(316): Illuminate\Routing\Route->getController()
#5 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Routing/Route.php(278): Illuminate\Routing\Route->controllerMiddleware()
#6 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Routing/Router.php(655): Illuminate\Routing\Route->gatherMiddleware()
#7 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Routing/Router.php(635): Illuminate\Routing\Router->gatherRouteMiddleware(Object(Illuminate\Routing\Route))
#8 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Routing/Router.php(618): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#9 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Routing/Router.php(596): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#10 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(267): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#11 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#12 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#13 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#14 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#16 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(149): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#17 /vagrant/myApp/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#18 /vagrant/myApp/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#19 {main}
[2016-12-22 16:51:13] local.ERROR: Symfony\Component\Debug\Exception\FatalErrorException: Cannot declare class App\Http\Controller\Admin\DashboardController, because the name is already in use in /vagrant/myApp/app/Http/Controllers/Admin/DashboardController.php:0
Stack trace:
#0 {main}
我尝试过使用不同的类名,但没有解决任何问题。只要我路由到Admin文件夹/命名空间中的控制器,我就会得到列出的错误。我在每次更改后都运行composer dump-autoload
并运行php artisan cache:clear
只是为了安全
我的文件夹结构
/app/
Console/
Exceptions/
Http/
Controllers/
Admin/
DashboardController.php
Auth/
Controller.php
...OtherController.php files live here in the base Controller folder
Middleware/
Providers/
/routes/web.php
...
// Admin Routes
Route::group( [ 'namespace' => 'Admin','prefix' => 'admin' ], function(){
Route::get( '/', 'DashboardController@showAdminHome' )->name( 'admin.dashboard' );
});
/app/Http/Controllers/Admin/DashboardController.php
<?php
namespace App\Http\Controller\Admin;
use App\Http\Controllers\Controller;
class DashboardController extends Controller {
public function showAdminHome() {
return view( 'admin.dashboard' );
}
} //- END Admin\DashboardController{}
我已经用尽了一些东西,尝试并感谢任何帮助。
答案 0 :(得分:3)
您需要使用正确的命名空间来修复错误:
namespace App\Http\Controllers\Admin;
答案 1 :(得分:0)
检查命名空间还在命名空间中添加完整路径。
命名空间App \ Http \ Controllers \ Admin;