[英]Call a method from one controller inside another
是否可以从Laravel 5中另一个控制器内的一个控制器调用一个方法(无论用于访问每个方法的http方法)?
This is how I have done it. 这就是我做到的。 Use the
use
keyword to make the OtherController available. 使用
use
关键字使OtherController可用。 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();
}
}
Also check out the concept of a Command in Laravel. 还可以查看Laravel中Command的概念。 It might give you more flexibility than the method above.
它可能比上面的方法更灵活。
use App\Http\Controllers\TargetsController;
// this controller contains a function to call
class OrganizationController extends Controller {
public function createHolidays() {
// first create the reference of this controller
$b = new TargetsController();
$mob = 9898989898;
$msg = "i am ready to send a msg";
// parameter will be same
$result = $b->mytesting($msg, $mob);
log::info('my testing function call with return value' . $result);
}
}
// this controller calls it
class TargetsController extends Controller {
public function mytesting($msg, $mob) {
log::info('my testing function call');
log::info('my mob:-' . $mob . 'my msg:-' . $msg);
$a = 10;
return $a;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.