简体   繁体   English

从另一个控制器内部调用方法

[英]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.

相关问题 如何将一个变量存储在一个控制器方法内的缓存中并在同一控制器中的另一种方法中访问它? - Laravel 6 - How to store one variable in a cache inside one controller method and access it in another method in the same controller? - Laravel 6 在Laravel中从同一控制器中的另一个方法模拟调用方法 - Mock call to method from another method within same controller in Laravel 如何在Laravel中的另一个控制器内调用控制器 - How to call a controller inside another controller in laravel 将一个控制器中的函数调用到另一个控制器 - Call function in one controller to another controller 将变量从一个函数传递到单个控制器中的另一个函数 - Pass variable from one function to another inside a single controller 在同一控制器 Laravel 中将变量从一种方法传递到另一种方法 - Pass variable from one method to another in same controller Laravel 将变量从一种方法传递到同一控制器中的另一种方法 - pass variable from one method to another within the same controller Laravel-一个控制器中另一个控制器的运行方法 - Laravel - Running method of another controller in one controller Laravel Controller-在另一个函数内调用函数 - Laravel Controller - call function inside another function LARAVEL - 无法在控制器方法中调用函数 - LARAVEL - Not able to call a function inside a controller method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM