Laravel Blank white page

2019-05-15 02:44发布

I'm having a problem with my get route in a group. here is my code:

Route::group(['prefix' => 'commodities'], function(){
    Route::get('commodities', [
        'as' => 'showCommodities', 'uses' => 'CommodityController@showAll'
    ]);

    Route::get('{id}', [
        'as' => 'showCommodity', 'uses' => 'CommodityController@show'
    ]);

    Route::get('add', [
        'as' => 'addCommodity', 'uses' => 'CommodityController@create'
    ]);

    Route::post('update', [
        'as' => 'updateCommodity', 'uses' => 'CommodityController@update'
    ]);

    Route::post('destroy', [
        'as' => 'destroyCommodity', 'uses' => 'CommodityController@destroy'
    ]);

    Route::post('add', [
        'as' => 'storeCommodity', 'uses' => 'CommodityController@store'
    ]);
});

I pasted the CommodityController code here http://pastebin.com/bWrdVhsv

Everything works except the GET route commodites/add. I always get a white page. My debug is set to TRUE and I have the correct blade for it.

Am I missing something here?

1条回答
虎瘦雄心在
2楼-- · 2019-05-15 03:22

The problem is the order of your routes.

Move the add route above your catch all {id} route.

Route::group(['prefix' => 'commodities'], function(){
    Route::get('commodities', [
        'as' => 'showCommodities', 'uses' => 'CommodityController@showAll'
    ]);

    Route::get('add', [
        'as' => 'addCommodity', 'uses' => 'CommodityController@create'
    ]);

    Route::get('{id}', [
        'as' => 'showCommodity', 'uses' => 'CommodityController@show'
    ]);

    Route::post('update', [
        'as' => 'updateCommodity', 'uses' => 'CommodityController@update'
    ]);

    Route::post('destroy', [
        'as' => 'destroyCommodity', 'uses' => 'CommodityController@destroy'
    ]);

    Route::post('add', [
        'as' => 'storeCommodity', 'uses' => 'CommodityController@store'
    ]);
});

Laravel will go through your routes.php file top to bottom. The below route is essentially a catch all.

Route::get('{id}', [
        'as' => 'showCommodity', 'uses' => 'CommodityController@show'
]);

This means it will catch all GET requests to urls that match the pattern:

/commodities/some-kind-of-string.

As the /commodities/add uri matches the above pattern it will use that route because it appears first in the routes file.

查看更多
登录 后发表回答