Laravel - 多个资源路由到带有参数的单个控制器

Laravel - multiple resource routes to single controller with argument

我们可以用一个控制器来控制多个路由,并获取参数吗?

目前,我有这些路线:

Route::resource('/customers', 'CustomerController');
Route::resource('/agents', 'AgentController');

还有一个 CustomerController 和一个 AgentController,所有资源功能都在工作。

但是 CustomerControllerAgentController 几乎相同,除了一个数据库字段,即 group_id。我正在考虑使用一个控制器,即 PartyController 和一条路线:

Route::resource('/parties/customers', 'PartyController ');
Route::resource('/parties/agents', 'PartyController ');

或者如果有人建议:

Route::resource('/parties/{group}', 'PartyController ');

我已经搜索了一段时间,但发现很难遵循这条路。我在 PartyController 的构造函数中添加了这段代码来检查调用路由:

    $path = Request::capture()->path();
    $this->group = ucwords(explode("/", $path)[1]);
    echo($this->group );

到目前为止一切似乎都很顺利。但是当在我的 index.blade.php 中时,我有这样的声明:

<p>{{ link_to_route('parties.create', 'Add new') }}</p>

我遇到异常:

Route [parties.create] not defined. 

我尝试了多种组合,没有任何成功,并且出现了更多错误,例如访问 /parties/customers/create 现在不起作用。

那么,有没有可能呢,还是我应该放弃这个想法?

编辑:我的问题与 不同,因为我没有使用特征。

如果我是你,我会使用:

Route::resource('/customers', 'CustomerController');
Route::resource('/agents', 'AgentController');

现在您可以 AgentController 扩展 CustomerController(或您想要的任何其他控制器),以便它们可以重用相同的代码。如果需要,您可以在构造函数中设置一些属性,例如组,以了解您是在与代理还是客户打交道。

为了使您的路线正常工作,您可以将额外的变量从控制器传递到视图,以便在您的 blade:

<p>{{ link_to_route($group.'.create', 'Add new') }}</p>
<p>{{ link_to_route('parties.create', 'Add new') }}</p>

你错过了你添加的变量参数,应该是

 <p>{{ link_to_route('parties.' . $group . '.create', 'Add new') }}</p>

正如我现在所想...使用 class 继承会很好! :D

您可以将 PartyController 定义为 CustomerControllerAgentController 的父级(它们都扩展了 PartyController)。

因此,将常用方法移至 PartyController 并从 Customer/Agent 控制器中调用它们。 :)

所以,首先你用下一行定义路线:

Route::resource('/parties/agents', PartiesAgentsController::class);
Route::resource('/parties/customers', PartiesCustomersController::class);

那么您可能需要一些 REST 风格的派对:

class PartiesController extends BaseController
{

    /**
     * The Entry Repository
     *
     * @var EntryRepositoryInterface
     */
    protected $repository;

    /**
     * Create an instance of the RestController class
     *
     * @param EntryRepositoryInterface $repository
     */
    public function __construct(EntryRepositoryInterface $repository)
    {
        parent::__construct();

        if (!$this->request->ajax())
        {
            return redirect('/');
        }

        $this->repository = $repository;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request    $request
     * @return \Illuminate\Http\Response
     */
    public function store()
    {
    }

    /**
     * Display the specified resource.
     *
     * @param  int                         $slug
     * @return \Illuminate\Http\Response
     */
    public function show($slug)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int                         $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request    $request
     * @param  int                         $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int                         $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
    }

}

在这个控制器的构造方法中,我正在做一些检查和其他操作,如您所见。我也使用存储库...它不是 DDD)。

然后,我可以像这样使用以上所有内容:

class PartiesAgentsController extends PartiesController
{

    /**
     * Create an instance of the PartiesAgentsController class
     *
     * @param EntryRepositoryInterface $repository
     */
    public function __construct(PartyAgentRepositoryInterface $repository)
    {
        parent::__construct($repository);
    }

}

客户也是如此...

我是用我自己的方法做的,我用的是同样的路线。

Route::resource('/parties/agents', 'PartyController');
Route::resource('/parties/customers', 'PartyController');

并且在 PartyController 的构造函数中

$path = Request::capture()->path();
$group = strtolower(explode("/", $path)[1]);

现在我有了 $group 变量来识别我正在使用哪个组。

在我的 blade 文档中,我将 parties.create 替换为 $group.create

<a href="{{ route($group.'.create')}}" > Add new </a>

其他路线也一样..现在一切如我所愿。

感谢大家的帮助。