Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gruz's avatar
Level 1

Call Laravel controller from code and pass HTTP headers to it

To call a controller I use a CURL request like this:

curl -kX POST https://site/method  -H 'Authorization: Bearer ACCESS_TOKEN\
Content-Type:application/json' -d '
{
    "param1":"value 1",
    "param2":"value 2",
    "param3":"value 3"
}'

The route calls UserController@method.

How can I do the same from Laravel code (internal request, not sending CURL)?

I've found an advice to use something like this

            $controller = app()->make($controllerName);
            $response = app()->call([$controller, 'method'], []);
            echo $response;

where the last [] should contain some parameters. But if this is the way, I cannot figure out how the array should look in my case.

0 likes
3 replies
cipsas's avatar

Maybe this works:

$response =  (new $controllerName)->methodName();

// get header data
$response->headers->all();

gruz's avatar
Level 1

I need to pass headers to the controller, not to get them.

Please or to participate in this conversation.