Reputation: 4741
I try to call a controller in an other controller with a parameter. I have no problem without any parameters.
return app()->call('App\Http\Controllers\AppointmentController@create');
AppointmentController.php
class AppointmentController extends Controller
{
public function create(CalendarInterface $calendar, Request $request) {
...
}
}
But I have an error when I try to pass one.
return app()->call('App\Http\Controllers\AppointmentController@create', $response);
AppointmentController.php
class AppointmentController extends Controller
{
public function create($response, CalendarInterface $calendar, Request $request) {
...
}
}
Type error: Argument 2 passed to Illuminate\Container\Container::call() must be of the type array, object given
Or if I do
return app()->call('App\Http\Controllers\AppointmentController@create', [$response]);
Type error: Argument 2 passed to App\Http\Controllers\AppointmentController::create() must implement interface App\CalendarInterface, instance of Illuminate\Http\Request given in [...]/app/Http/Controllers/AppointmentController.php:18
Upvotes: 3
Views: 5716
Reputation: 16373
The Laravel's automagic DI probably doesn't support this use case, passing only parts of the arguments and asking him to guess what remains to be resolved.
Try passing all the arguments:
app()->call(
'App\Http\Controllers\AppointmentController@create',
[$response, app(CalendarInterface::class), app(Request::class)]
);
As a side note, calling such a "sub-controller" is probably a bad practice. How about moving the business logic to plain classes?
Upvotes: 0
Reputation: 40653
Use named parameters:
app()->call('App\Http\Controllers\AppointmentController@create', [
"response" => $response
]);
Upvotes: 3