从一个控制器调用另一个控制器中的方法

Call a method from one controller inside another

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

我就是这样做的。使用 use 关键字使 OtherController 可用。然后您可以在实例化时从 class 调用一个方法。

<?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 中 Command 的概念。它可能比上述方法更灵活。

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