【问题标题】:Multiple routes to same controller多个路由到同一个控制器
【发布时间】:2015-04-22 16:20:31
【问题描述】:

我正在尝试构建一个完整的 API,我想到了一个点,我认为,并且 REST 对它不是很明确,一个资源应该可以通过多个 URL 访问。举个例子吧。

Route::group(['prefix' => 'forum'], function(){

    Route::resource('categories', 'CategoriesController');

    Route::resource('categories.discussions', 'DiscussionsController');

    Route::resource('discussions', 'DiscussionsController');

});

我的目的是通过将讨论附加到它所属的类别来访问每个讨论:

localhost/forum/categories/{category_id}/discussions

或者通过这个网址

本地主机/论坛/讨论

有没有人可以在不重写完整控制器的情况下完成类似的工作。

这是我的讨论控制器:

class DiscussionsController extends ApiController {


public function __construct(DiscussionRepositoryInterface $discussion)
{
    $this->discussions = $discussion;
}

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index($category = null)
{
    $response = $this->discussions->all($category);

    return $this->respond($response->getData());
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    if (!$this->hasPermission('forum.create.discussion'))
    {
        return $this->respondNotAllowed();
    }

    return $this->respondSuccess('Granted');
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store($category)
{
    if (!$this->hasPermission('forum.create.discussion'))
    {
        return $this->respondNotAllowed();
    }

    return $this->respondSuccess('Granted');

}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($categories = null, $discussions)
{
    $discussion =  $this->discussions->find($categories, $discussions);

    if ($discussion == null)
        return $this->respondNotFound('Discussion not found');

    $data = [
        'discussions' => $discussion
    ];

    return $this->respond($data);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    if (!$this->hasPermission('forum.edit.discussion'))
    {
        return $this->respondNotAllowed();
    }

    return $this->respondSuccess('Granted');
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id, $data)
{

    if (!$this->hasPermission('forum.edit.discussion'))
    {
        return $this->respondNotAllowed();
    }

    $data = Input::only('category_id', 'user_id', 'desc', 'title');

    $data = $this->clearNullInput($data);

    if ($this->discussions->update($id, $data))
        return $this->respondError('Couldnt update discussion');

    return $this->respondSuccess('Discussion updated successfully');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    if (!$this->hasPermission('forum.delete.discussion'))
    {
        return $this->respondNotAllowed();
    }

    if (!$this->discussions->delete($id))
        return $this->respondError('Couldnt destroy user');

    return $this->respondSuccess('Granted');
}

}

index 方法在两个调用中都可以正常工作(localhost/forum/categories/id/discussions 和 localhost/forum/discussions)

但是当我尝试显示资源时,只有长版本有效,短版本会抛出以下异常:

ErrorException in DiscussionsController.php line 68:
Missing argument 2 for App\Http\Controllers\DiscussionsController::show()

我会说 laravel 发疯了,因为他无法识别 id,有什么聪明的解决方法还是我必须重写控制器?

任何意见都非常感谢,谢谢

【问题讨论】:

    标签: php rest laravel


    【解决方案1】:

    你可以试试这样的

    public function show(Category $category, Discussion $discussion)
    

    如果您调用localhost/forum/categories/{categories}/discussions/{discussions},那么这两个变量都有它们的确切值。如果您致电localhost/forum/discussions/{discussions},则类别将只是新类别

    确保将 RouteServiceProvider 中的值绑定为

    $router->model('discussions', 'App\Discussion');
    $router->model('categories', 'App\Category');
    

    【讨论】:

    • 工作就像一个魅力!一开始我以为我会遇到与没有模型绑定相同的问题,我想这就是 Laravel 的魔力
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2015-09-03
    • 2020-02-27
    • 2012-01-08
    • 2020-02-19
    • 2012-05-14
    相关资源
    最近更新 更多