Laravel - 根目录路由在嵌套路由组中表现奇怪

时间:2016-02-08 03:58:42

标签: php laravel routing

我在Laravel 5中有以下路由定义。当我按以下方式对路由进行分组时,如果我提供admin.proposals.home作为获取路径,则路由"/"似乎不起作用,并且如果提供了一些尾随的内容(如home),则可以正常工作:

/** 
 *  Routes for system administrators.
*/
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
    Route::group(['prefix' => 'people'], function() 
    {
        Route::get('/', ['as' => 'admin.people.home', 'uses' => 'PersonController@index']);
        Route::get('/profile/{userName}', ['as' => 'admin.person.profile', 'uses' => 'PersonController@view']);
        Route::get('/organization/{id}', ['as' => 'admin.people.organization', 'uses' => 'PersonController@indexByOrganization']);
    });

    Route::group(['prefix' => 'projects'], function() 
    {
        Route::get('/', ['as' => 'admin.projects.home', 'uses' => 'ProjectController@index']);
        Route::get('/{projectId}', ['as' => 'admin.project.view', 'uses' => 'ProjectController@view']);

        Route::group(['prefix' => 'proposals'], function() 
        {
            //problematic line below
            Route::get('/home', ['as' => 'admin.proposals.home', 'uses' => 'ProposalController@index']);   
            Route::get('/{proposalId}', ['as' => 'admin.proposal.view', 'uses' => 'ProposalController@view']);
        });

    });

});  

具体来说,如果我改变了行:

Route::get('/home', ['as' => 'admin.proposals.home', 
           'uses' => 'ProposalController@index'
          ]);  

致:

Route::get('/', ['as' => 'admin.proposals.home', 
            'uses' => 'ProposalController@index'
          ]);  

我收到错误说:

Trying to get property of non-object (View:  ... \views\admin\projects\view.blade.php)

但是,admin.proposals.home路由指向控制器index()方法,与view.blad.php无关。

将路径更改回get('/home')非常有效。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

这是Route位置订单问题。

由于您将URI admin/projects/的路由设为admin.project.home,之后admin/projects/{projectId}优先于路由admin/projects/proposals/

Laravel将proposals作为projectId

    Route::group(['prefix' => 'projects'], function() 
    {
        Route::group(['prefix' => 'proposals'], function() 
        {
            //problematic line below
            Route::get('/', ['as' => 'admin.proposals.home', 'uses' => 'ProposalController@index']);   
            Route::get('/{proposalId}', ['as' => 'admin.proposal.view', 'uses' => 'ProposalController@view']);
        });
        Route::get('/', ['as' => 'admin.projects.home', 'uses' => 'ProjectController@index']);
        Route::get('/{projectId}', ['as' => 'admin.project.view', 'uses' => 'ProjectController@view']);



    });

试试此订单,让我知道你得到了什么。但纯粹是路线订单问题。