1. <small id='Dq6Mh'></small><noframes id='Dq6Mh'>

      <i id='Dq6Mh'><tr id='Dq6Mh'><dt id='Dq6Mh'><q id='Dq6Mh'><span id='Dq6Mh'><b id='Dq6Mh'><form id='Dq6Mh'><ins id='Dq6Mh'></ins><ul id='Dq6Mh'></ul><sub id='Dq6Mh'></sub></form><legend id='Dq6Mh'></legend><bdo id='Dq6Mh'><pre id='Dq6Mh'><center id='Dq6Mh'></center></pre></bdo></b><th id='Dq6Mh'></th></span></q></dt></tr></i><div id='Dq6Mh'><tfoot id='Dq6Mh'></tfoot><dl id='Dq6Mh'><fieldset id='Dq6Mh'></fieldset></dl></div>

      <tfoot id='Dq6Mh'></tfoot>
      • <bdo id='Dq6Mh'></bdo><ul id='Dq6Mh'></ul>
      <legend id='Dq6Mh'><style id='Dq6Mh'><dir id='Dq6Mh'><q id='Dq6Mh'></q></dir></style></legend>

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

        (Laravel) How to use 2 controllers in 1 route?((Laravel)如何在 1 条路线中使用 2 个控制器?)

                  <tbody id='Ryv3H'></tbody>
              • <small id='Ryv3H'></small><noframes id='Ryv3H'>

                <tfoot id='Ryv3H'></tfoot>
                <i id='Ryv3H'><tr id='Ryv3H'><dt id='Ryv3H'><q id='Ryv3H'><span id='Ryv3H'><b id='Ryv3H'><form id='Ryv3H'><ins id='Ryv3H'></ins><ul id='Ryv3H'></ul><sub id='Ryv3H'></sub></form><legend id='Ryv3H'></legend><bdo id='Ryv3H'><pre id='Ryv3H'><center id='Ryv3H'></center></pre></bdo></b><th id='Ryv3H'></th></span></q></dt></tr></i><div id='Ryv3H'><tfoot id='Ryv3H'></tfoot><dl id='Ryv3H'><fieldset id='Ryv3H'></fieldset></dl></div>

                <legend id='Ryv3H'><style id='Ryv3H'><dir id='Ryv3H'><q id='Ryv3H'></q></dir></style></legend>
                  <bdo id='Ryv3H'></bdo><ul id='Ryv3H'></ul>
                • 本文介绍了(Laravel)如何在 1 条路线中使用 2 个控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  How can I use 2 controllers in 1 route?

                  这里的目标是创建多个页面,每个页面都有 1 个职业(例如:会计师),然后将它们链接到提供会计课程的学校.

                  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. 会计师职业信息(我在这里使用职业"控制器)
                  2. 提供会计课程的学校(我在这里使用单独的学校"控制器).

                  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).

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

                  使用上面的代码将覆盖控制器中的 1 个.

                  Using the code above will overwrite 1 of the controllers.

                  有没有办法解决这个问题?

                  Is there a solution to solve this?

                  推荐答案

                  你不能那样做,因为这不是一件好事,而且 Laravel 不会让你有相同的路线来点击两个不同的控制器动作, 除非您使用不同的 HTTP 方法(POST、GET...).Controller 是一个 HTTP 请求处理程序,而不是一个服务类,所以你可能需要稍微改变你的设计,这是一种方法:

                  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:

                  如果您要在一页中显示所有数据,请创建一个路由器:

                  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());
                      }
                  
                  }
                  

                  并创建一个DataRepository类,负责知道在需要那种数据的情况下该怎么做:

                  And create a DataRepository class, responsible for knowing what to do in the case of need that kind of data:

                  class DataRepository {
                  
                      public getData()
                      {
                          $data = array();
                  
                          $data['accountant'] = Accountant::all();
                  
                          $data['schools'] = School::all();
                  
                          return $data;
                      }
                  
                  }
                  

                  请注意,此存储库会自动注入到您的控制器中,Laravel 会为您完成.

                  Note that this repository is being automatically inject in your controller, Laravel does that for you.

                  这篇关于(Laravel)如何在 1 条路线中使用 2 个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <i id='bHwGD'><tr id='bHwGD'><dt id='bHwGD'><q id='bHwGD'><span id='bHwGD'><b id='bHwGD'><form id='bHwGD'><ins id='bHwGD'></ins><ul id='bHwGD'></ul><sub id='bHwGD'></sub></form><legend id='bHwGD'></legend><bdo id='bHwGD'><pre id='bHwGD'><center id='bHwGD'></center></pre></bdo></b><th id='bHwGD'></th></span></q></dt></tr></i><div id='bHwGD'><tfoot id='bHwGD'></tfoot><dl id='bHwGD'><fieldset id='bHwGD'></fieldset></dl></div>
                      <tfoot id='bHwGD'></tfoot>

                      • <small id='bHwGD'></small><noframes id='bHwGD'>

                            <tbody id='bHwGD'></tbody>
                          • <bdo id='bHwGD'></bdo><ul id='bHwGD'></ul>

                          • <legend id='bHwGD'><style id='bHwGD'><dir id='bHwGD'><q id='bHwGD'></q></dir></style></legend>