简体   繁体   English

(Laravel)如何在1条路线中使用2个控制器?

[英](Laravel) How to use 2 controllers in 1 route?

How can I use 2 controllers in 1 route? 如何在1条路线中使用2个控制器?

The goal here is to create multiple pages with 1 career each (eg: Accountants) then link them to a school providing an Accounting course. 这里的目标是创建多个页面,每个页面有1个职业(例如:会计师),然后将它们链接到提供会计课程的学校。

An example page would consist of: 示例页面包括:
1. Accountants career information (I'm using a "career" controller here) 1.会计师职业信息(我在这里使用“职业”控制员)
2. Schools providing Accounting courses (I'm using a separate "schools" controller here). 2.提供会计课程的学校(我在这里使用单独的“学校”控制员)。

Route::get('/accountants-career', 'CareerController@accountants');
Route::get('/accountants-career', 'SchoolsController@kaplan');

Using the code above will overwrite 1 of the controllers. 使用上面的代码将覆盖其中一个控制器。

Is there a solution to solve this? 有解决方案可以解决这个问题吗?

You cannot do that, because this is not a good thing to do and by that Laravel don't let you have the same route to hit two different controllers actions, unless you are using different HTTP methods (POST, GET...). 你不能这样做,因为这不是一件好事,并且Laravel不会让你有相同的路由来命中两个不同的控制器动作,除非你使用不同的HTTP方法(POST,GET ......)。 A Controller is a HTTP request handler and not a service class, so you probably will have to change your design a little, this is one way of going with this: Controller是HTTP请求处理程序而不是服务类,因此您可能需要稍微更改一下设计,这是一种方法:

If you will show all data in one page, create one single router: 如果要在一个页面中显示所有数据,请创建一个路由器:

Route::get('/career', 'CareerController@index');

Create a skinny controller, only to get the information and pass to your view: 创建一个瘦控制器,只是为了获取信息并传递给您的视图:

use View;

class CareerController extends Controller {

    private $repository;

    public function __construct(DataRepository $repository)
    {
        $this->repository = $repository;
    }

    public function index(DataRepository $repository)
    {
        return View::make('career.index')->with('data', $this-repository->getData());
    }

}

And create a DataRepository class, responsible for knowing what to do in the case of need that kind of data: 并创建一个DataRepository类,负责在需要那种数据的情况下知道该做什么:

class DataRepository {

    public getData()
    {
        $data = array();

        $data['accountant'] = Accountant::all();

        $data['schools'] = School::all();

        return $data;
    }

}

Note that this repository is being automatically inject in your controller, Laravel does that for you. 请注意,此存储库正在您的控制器中自动注入,Laravel会为您执行此操作。

Is there a specific reason you need to use the same route name? 您是否需要使用相同的路线名称? Currently you have no way of telling the routes apart to laravel when it processes them. 目前,在处理它们时,你无法将路线分开来说明laravel。

why not something such as; 为什么不是这样的;

Route::get('/accountants/career', 'CareerController@accountants');
Route::get('/accountants/schools', 'SchoolsController@kaplan');

you could also do something like this if you have multiple careers going to the same controller and methods based on their value. 如果你有多个职业基于他们的价值去同一个控制器和方法,你也可以做这样的事情。 This allows you to have a separate call you can call for each of your approved values rather than having to set a separate route and controller method for each. 这允许您可以为每个已批准的值调用单独的调用,而不必为每个值设置单独的路由和控制器方法。

Route::get('/{careerName}/career', 'CareerController@all');
Route::get('/{careerName}/schools', 'SchoolsController@kaplan');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM