【发布时间】: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