Laravel app :: call('SomeController @ method')不起作用

问题描述 投票:1回答:2

我希望能够以下列方式调用控制器内的方法:

App::call('SomeController@method');

我认为在定义路线时会发生这种情况:

Route::get('/route', 'SomeController@method');

所以也许我可以在项目中其他地方的代码中更普遍地使用一种底层机制,结果是(App :: call())。

我遇到的问题是这会产生一个错误:

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();

我很确定我必须在某个地方加入一些东西但是因为Laravel相当大,所以我问这个社区。

php laravel laravel-5
2个回答
1
投票

尝试给出完整的命名空间,如:

App::call('App\Http\Controllers\SomeController@method');

0
投票

您可以使用app()帮助程序调用该方法。语法是app($fullControllerClassName)->methodYouWantToCall()

app('App\Http\Controllers\SomeController')->method();
© www.soinside.com 2019 - 2024. All rights reserved.