从另一个控制器中调用一个方法 [英] Call a method from one controller inside another

查看:1370
本文介绍了从另一个控制器中调用一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从Laravel 5中的另一个控制器中的一个控制器调用方法(不考虑用于访​​问每个方法的http方法)?

Is it possible to call a method from one controller inside another controller in Laravel 5 (regardless the http method used to access each method)?

推荐答案

这是我怎么做的。使用使用关键字使OtherController可用。

This is how I have done it. Use the use keyword to make the OtherController available. Then you can call a method from that class on instantiation.

<?php namespace App\Http\Controllers;

use App\Http\Controllers\OtherController;

class MyController extends Controller {

    public function __construct()
    {
        //Calling a method that is from the OtherController
        $result = (new OtherController)->method();
    }
}

同时检查命令在Laravel。它可能比上面的方法更灵活。

Also check out the concept of a Command in Laravel. It might give you more flexibility than the method above.

这篇关于从另一个控制器中调用一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆