Laravel 6 Using `$controller->callAction` To Send Data To Another Controller

- 1 answer

I have 2 controller and I send data to second controller using callAction. This is what I have in my first controller

$controller = App::make('\App\Http\Controllers\EmailController');
$email = $controller->callAction('sendEmail', array('firstitem'=>'qweqw','seconditem' =>'asd'));

with the above code im expecting to get an array in the second controller. In my second controller I have

public function sendEmail($request){

        return ($request);

to check if i will get an array, but im getting only: qweqw which only the first item of the array.

What is the correct way to send or receive an array in this scenario



The following line should work.


app('...') is an alias for App::make('...'). The issue with your code is the callAction method.

You're already getting an instance of the App\Http\Controller\EmailController when you use app('...').

The controller is, above all, a Class, and as such, you can use any public method it has, such as sendEmail by calling it directly and passing parameters as needed. (->sendEmail($params)).