'Laravel app::call('SomeController@method') not working
I want to be able to call a method inside a controller in the following manner:
App::call('SomeController@method');
I figured it happens like this when defining a route:
Route::get('/route', 'SomeController@method');
So maybe there's an underlying mechanism I can use more generally in my code on other places in the project, turns out there is (App::call()).
The problem I'm running into is that this generates an error:
ReflectionException (-1)
Class SomeController does not exist
## \vendor\laravel\framework\src\Illuminate\Container\Container.php
public function build($concrete)
{
// If the concrete type is actually a Closure, we will just execute it and
// hand back the results of the functions, which allows functions to be
// used as resolvers for more fine-tuned resolution of these objects.
if ($concrete instanceof Closure) {
return $concrete($this, $this->getLastParameterOverride());
}
$reflector = new ReflectionClass($concrete);
// If the type is not instantiable, the developer is attempting to resolve
// an abstract type such as an Interface of Abstract Class and there is
// no binding registered for the abstractions so we need to bail out.
if (! $reflector->isInstantiable()) {
return $this->notInstantiable($concrete);
}
$this->buildStack[] = $concrete;
$constructor = $reflector->getConstructor();
I'm pretty sure I must include some stuff somewhere but since Laravel is pretty big I'm asking this community.
Solution 1:[1]
You can call the method by using the app() helper. Syntax is app($fullControllerClassName)->methodYouWantToCall():
app('App\Http\Controllers\SomeController')->method();
Solution 2:[2]
Another way is:
app()->call(['\App\Http\Controllers\SomeController', 'index']);
where index is the name of the method. You can add a second parameter to send some data like so:
app()->call(['\App\Http\Controllers\SomeController', 'index'], [
'name' => 'Strawberry'
]);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexey Mezenin |
| Solution 2 | Eskay Amadeus |
