【问题标题】:Laravel 4 : Route returning wrong informationLaravel 4:路由返回错误信息
【发布时间】:2014-05-20 22:59:11
【问题描述】:

我正在使用 Laravel 设置一个 API,以便我可以连接 AngularJS 前端。

我有 2 条路由到同一个控制器方法 -> BallController@getSpecs

我的路线设置如下:

Route::group(['prefix' => '/api/v1', 'before' => 'auth.basic'], function() {
    Route::group(['prefix' => '/ball'], function() {
        Route::get('/', 'BallController@getIndex');
        Route::get('{id}', 'BallController@getIndex');
        Route::get('specs', 'BallController@getSpecs');
        Route::get('{id}/specs', 'BallController@getSpecs');
    });
});

现在我遇到了Route::get('specs', 'BallController@getSpecs'); 路由问题。

getSpecs 方法定义如下:

public function getSpecs($id = null) 
{
    if(empty($id))
    {
        Ball::all()->each(function($ball) {
            $json = [$ball->id => [
                'name'          => $ball->name,
                'core'          => $ball->core->toArray(),
                'coverstock'    => $ball->coverstock->toArray(),
                'finish'        => $ball->finish->toArray(),
                'manufacturer'  => $ball->manufacturer->toArray()
            ]];
        });

        return Response::json($json);
    }
    else
    { 
        $ball = Ball::find($id);

        if(empty($ball))
        {
            return Response::json('You\'ve got no ballss', 404);
        }
        else
        {
            return Response::json([$ball->id => [
                'name'          => $ball->name,
                'core'          => $ball->core->toArray(),
                'coverstock'    => $ball->coverstock->toArray(),
                'finish'        => $ball->finish->toArray(),
                'manufacturer'  => $ball->manufacturer->toArray()
            ]]);
        }
    }
}

当我调用 /api/v1/ball/1/specs 并指定一个 id 时,我得到了正确的信息,但是当我调用 /api/v1/ball/specs 时,我的函数返回我的错误消息“你没有球”

ID 应该在这个演员表中为空,将我放入 if 语句的第一部分,但由于某种原因,我进入了 else 并得到了我的错误,因为显然没有提供 ID 并且球将不存在。

任何帮助/见解将不胜感激。

编辑:我认为它可能将其发送到错误的方法。我认为/api/v1/ball/specs 被发送到BallController@getIndex 而不是BallController@getSpecs。

【问题讨论】:

    标签: laravel laravel-4 laravel-routing


    【解决方案1】:

    问题出在这部分路由:

    Route::get('{id}', 'BallController@getIndex');
    Route::get('specs', 'BallController@getSpecs');
    

    当它看到“/api/v1/ball/specs”时,它实际上会调用 getIndex() 并将“id”参数设置为“specs”,因为这是第一个匹配的路由。解决它的一种方法是在定义“{id}”路由之前先定义“specs”路由。但更好的解决方案是像这样限制“{id}”参数的接受值......

    Route::get('{id}', 'BallController@getIndex')->where('id', '[0-9]+');
    

    另一个建议是,您的前缀值中不应有前导“/”(显然代码可能仍然有效,但它们实际上不应该存在。所以完整的更新路线可能看起来像这样...

    Route::group(['prefix' => 'api/v1', 'before' => 'auth.basic'], function() {
        Route::group(['prefix' => 'ball'], function() {
            Route::get('/', 'BallController@getIndex');
            Route::get('specs', 'BallController@getSpecs');
            Route::get('{id}', 'BallController@getIndex')->where('id', '[0-9]+');
            Route::get('{id}/specs', 'BallController@getSpecs')->where('id', '[0-9]+');
        });
    });
    

    【讨论】:

    • 谢谢!问题解决了。您是否可以推荐任何文档或阅读材料以更好地理解它为什么尝试将规范作为 {id} 传递?
    • 好吧,laravel.com/docs/routing。但基本上你只需要意识到路由器不知道“id”必须是一个整数,除非你明确告诉它。因此,路由器没有任何理由不会认为“/api/v1/ball/specs”的“specs”部分应该是您的 getIndex 函数的 id。
    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 2014-12-21
    • 2015-05-27
    • 2018-04-30
    • 2013-05-14
    • 1970-01-01
    • 2014-06-21
    • 2019-04-20
    相关资源
    最近更新 更多