Laravel调试404路由



好的,我使用的完整的routes.php文件…粘贴到这里:http://pastebin.com/kaCP3NwK

routes.php

//The route group for all other requests needs to validate admin, model, and add assets
Route::group(array('before' => 'validate_admin|validate_model'), function()
{
    //Model Index
    Route::get('admin/(:any)', array(
        'as' => 'admin_index',
        'uses' => 'admin@index'
    ));

管理员配置:

...
'models' => array(
'news' => array(
    'title' => 'News',
    'single' => 'news',
    'model' => 'AdminModels\News',
),
...

链接生成器:

@foreach (Config::get('administrator.models') as $key => $model)
    @if (AdminLibrariesModelHelper::checkPermission($key))
        <?php $key = is_numeric($key) ? $model : $key; ?>
        <li>
            {{ HTML::link(URL::to_route('admin_index', array($key)), $model['title']) }}
        </li>
    @endif
@endforeach

控制器/admin.php

public function action_index($modelName)
{
    //first we get the data model
    $model = ModelHelper::getModelInstance($modelName);
    $view = View::make("admin.index",
        array(
            "modelName" => $modelName,
        )
    );
    //set the layout content and title
    $this->layout->modelName = $modelName;
    $this->layout->content = $view;
}

因此,当访问http://example.com/admin/news时,news被送到action_index…但由于某些原因它没有到达,它返回404

注意,我定义了以下'model' => 'AdminModels\News',

实际上我的namespace寄存器是AdminModels,所以将它设置为'model' => 'AdminModels\News',为404

路由是按照注册的顺序求值的,所以(:any)路由应该排在最后。你被发送(我认为)admin@index -如果该函数尚未定义,这就是为什么你得到一个404。

与这个问题无关,但是如果有人(像我一样)来这里寻找为什么Laravel应用程序显示404的线索,这里有一些原因:

  • 在URL
  • 中指定的数据库中找不到模型
  • 你在RouteServiceProvider中设置了不正确的路由模型绑定(就像我偶然发现这个答案时所做的那样)。示例:Route::model('user', Tenant::class);应该是User::class
  • 一些中间件返回404(例如通过"abort(404)")
  • 对应控制器返回404
  • 没有找到控制器方法(或命名空间)(这是这个问题的答案)

最新更新