Last active
March 31, 2023 04:11
-
-
Save nasirkhan/809eee36875dc7c1935bc4289be214c6 to your computer and use it in GitHub Desktop.
Call Laravel Controller methods via command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' |
udibagas
commented
Dec 29, 2020
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]);
👍
I am using this way to add request parameters and call controller.
- add parameters
app()
->make('Illuminate\Http\Request')
->merge([
'search' => 'hello',
]);
- 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']
}
}
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