【发布时间】: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 将不起作用,因为该方法是静态调用的。