【问题标题】:Laravel 5 method does not exist in Controller while used PHP __callStatic method使用 PHP __callStatic 方法时控制器中不存在 Laravel 5 方法
【发布时间】:2016-11-08 10:37:02
【问题描述】:

我在 PHP 5.5.9 上使用 Laravel 5.2

我使用 PHP __callStatic 方法来动态添加功能,而不是以下控制器中的硬编码方法。当我从控制台尝试时它工作正常但是在调用方法路由时,我收到以下错误

方法 App\Http\Controllers\showCategory::latest() 不存在

这是我的路线

Route::get('Category/{id}',               'showCategory@latest');

这是我的控制器

class showCategory extends Controller
{

public $methods = [
    'latest'        => 'created_at', 
    'newArrival'    => 'created_at', 
    'mostViewed'    => 'views'
];

public function get( $link_or_id, $orderBy = 'created_at' )
{

}

public static function __callStatic($func, $arg)
{

    $category = new self();

    if( array_key_exists( $func, $category->methods ) )
    {

        return $category->get( $arg[0], $category->methods[ $func ] );

    }

} 

}

任何帮助指出我搞砸了?

【问题讨论】:

  • 考虑 Laravel 是在对象还是静态上下文中调用控制器函数
  • 尝试将public static function __callStatic更改为public function __call
  • __call 将不起作用,因为该方法是静态调用的。

标签: php laravel laravel-5


【解决方案1】:

__callStatic() 在静态上下文中调用不可访问的方法时触发。

showCategory::latest();它有效

从逻辑上讲,您如何将最新方法放入路由中,并且您知道是否找到它,如果您放入路由,则必须在控制器中找到它

【讨论】:

  • 当我从控制台访问此方法时,它按预期工作,但不是从路由。我不知道为什么。这就是我想弄清楚的答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
相关资源
最近更新 更多