laravel 5.1中的隐式模型绑定描述非常差。当我尝试在手册中做的时候,我总是会收到错误ReflectionException in RouteDependencyResolverTrait.php line 81: Class App\Http\Controllers\App\News does not exist
当然这是名称空间问题,但我无法理解修复它。我试着评论$namespace
,但这对我没有帮助。
<?php namespace App\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
protected $namespace = 'App\Http\Controllers';
public function boot(Router $router)
{
parent::boot($router);
$router->model('newsitem', 'App\News');
}
这是错误描述
Whoops, looks like something went wrong.
1/1 ReflectionException in RouteDependencyResolverTrait.php line 81: Class App\Http\Controllers\App\News does not exist
in RouteDependencyResolverTrait.php line 81
at ReflectionParameter->getClass() in RouteDependencyResolverTrait.php line 81
at ControllerDispatcher->transformDependency(object(ReflectionParameter), array('newsitem' => object(News)), array('newsitem' => object(News))) in RouteDependencyResolverTrait.php line 61
at ControllerDispatcher->resolveMethodDependencies(array('newsitem' => object(News)), object(ReflectionMethod)) in RouteDependencyResolverTrait.php line 44
at ControllerDispatcher->resolveClassMethodDependencies(array('newsitem' => object(News)), object(NewsController), 'getItem') in ControllerDispatcher.php line 162
at ControllerDispatcher->call(object(NewsController), object(Route), 'getItem') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(NewsController), object(Route), object(Request), 'getItem') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\NewsController', 'getItem') in Route.php line 203
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 708
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 710
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 675
at Router->dispatchToRoute(object(Request)) in Router.php line 635
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
答案 0 :(得分:0)
对我来说这看起来不对:
App\Http\Controllers\App\News
除非您在App \ Http \ Controllers中有一个名为App
的子目录。
如果您的Controllers目录中有App
目录,请确保您的路径文件具有正确的名称间距
Route::group([
'namespace' => 'App',
], function () {
// News resourceful controller
Route::resource('/','NewsController');
});
如果这仍然没有帮助,您可以发布控制器的示例吗?
答案 1 :(得分:0)
这是有效的解决方案
RouteServiceProvider.php
namespace App\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Route;
use App\News;
class RouteServiceProvider extends ServiceProvider
{
protected $namespace = 'App\Http\Controllers';
public function boot(Router $router)
{
parent::boot($router);
Route::bind('newsitem', function($item) {
return News::whereSef($item)->firstOrFail();
});
}
routes.php文件
Route::get('news/{newsitem}', ['as' => 'news.item', 'uses' => 'NewsController@getItem']);
NewsController.php
public function getItem($newsitem)
{
dd($newsitem);
}