从一个控制器内部调用另一个方法 [英] Call a method from one controller inside another
本文介绍了从一个控制器内部调用另一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在 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)?
推荐答案
我就是这样做的.使用 use
关键字使 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 AppHttpControllers;
use AppHttpControllersOtherController;
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屋!
查看全文