(Laravel) 如何在 1 个路由中使用 2 个控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26091998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(Laravel) How to use 2 controllers in 1 route?
提问by Stanley Tan
How can I use 2 controllers in 1 route?
如何在 1 个路由中使用 2 个控制器?
The goal here is to create multiple pages with 1 career each (e.g: 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)
2. Schools providing Accounting courses (I'm using a separate "schools" controller here).
示例页面将包括:
1. 会计师职业信息(我在此处使用“职业”控制器)
2. 提供会计课程的学校(我在此处使用单独的“学校”控制器)。
Route::get('/accountants-career', 'CareerController@accountants');
Route::get('/accountants-career', 'SchoolsController@kaplan');
Using the code above will overwrite 1 of the controllers.
使用上面的代码将覆盖 1 个控制器。
Is there a solution to solve this?
有没有办法解决这个问题?
回答by Antonio Carlos Ribeiro
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...). 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:
你不能这样做,因为这不是一件好事,而且 Laravel 不会让你有相同的路由来点击两个不同的控制器操作,除非你使用不同的 HTTP 方法(POST、GET...)。控制器是一个 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 会为您完成此操作。
回答by clonerworks
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');