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

2023-10-31php开发问题
4

本文介绍了(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 个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17