Skip to content

Instantly share code, notes, and snippets.

@nasirkhan
Last active March 31, 2023 04:11
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nasirkhan/809eee36875dc7c1935bc4289be214c6 to your computer and use it in GitHub Desktop.
Save nasirkhan/809eee36875dc7c1935bc4289be214c6 to your computer and use it in GitHub Desktop.
Call Laravel Controller methods via command line
<?php
php artisan tinker
$controller = app()->make('App\Http\Controllers\MyController');
app()->call([$controller, 'myMethodName'], []);
//the last [] in the app()->call() can hold arguments such as [user_id] => 10 etc'
@victorcbr
Copy link

nice !!
thanks, help me a lot.

i do this too with the command:
app()->call('App\Http\Controllers\MyController@myMethodName');

regards

@haridasshinde
Copy link

thank you !!
very helpfull for me.

@vitasya
Copy link

vitasya commented Jun 5, 2018

Hello, this approach is great, but how to pass request parameters?
I created Request object by this:
$request = Request::create('/controller/action', 'POST', $params);

@ankita18wgt
Copy link

Its helpful app()->call('App\Http\Controllers\MyController@myMethodName');
Thanks

@aaaaronjmnz
Copy link

how about if Im passing in a request? it wont accept it.

@var618
Copy link

var618 commented Jul 14, 2020

Hello, this approach is great, but how to pass request parameters?
I created Request object by this:
$request = Request::create('/controller/action', 'POST', $params);

I have searched long time in SO.
However I still dont know how to pass a customed Request object to app()->call() function.
Need help.

@var618
Copy link

var618 commented Jul 14, 2020

params

I do not try this, but I think it may work.
We must specify the controller method with injected Request $request, so we can generate a customed Request object, and then

$response = app(IndexController:class)->{'index'}($request)

Thus the $request object can pass to controller method.
The point, you cannot use request() helper function in your controller, It will create new Request, and cannot handler your input.
That's it.

@nasirkhan
Copy link
Author

I will try that approach, thanks.

@udibagas
Copy link

how about if Im passing in a request? it wont accept it.

$request = Illuminate\Http\Request(['params' => 'value', 'another_params' => 'another value']);
$controller = app()->make('App\Http\Controllers\SomeController');
app()->call([$controller, 'someMethod'], ['request' => $request]);

@brndnmg
Copy link

brndnmg commented May 3, 2021

how about if Im passing in a request? it wont accept it.

$request = Illuminate\Http\Request(['params' => 'value', 'another_params' => 'another value']);
$controller = app()->make('App\Http\Controllers\SomeController');
app()->call([$controller, 'someMethod'], ['request' => $request]);

👍

@semiherdogan
Copy link

I am using this way to add request parameters and call controller.

  1. add parameters
app()
  ->make('Illuminate\Http\Request')
  ->merge([
    'search' => 'hello',
  ]);
  1. call controller
$controller = app()->make('App\Http\Controllers\SearchController');
app()->call([$controller, 'index']);

-- Class

class SearchController
{
    public function index(Request $request)
    {
        return $request->all();
        // returns ['search' => 'hello']
    }
}

@epicsagas
Copy link

What about this?

$request = (new \Illuminate\Http\Request)->replace(['foo' => 'bar'])
$response = (new App\Http\Controller\SearchController)->foo($request);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment