【发布时间】:2020-04-04 06:33:13
【问题描述】:
在路由器/web.php 中:
Route::get('/{page}/{id}/{seo_title}', "Router@get");
在作为控制器的路由器类中:
public function get($page,$id,$seo_title)
{
$view_arg = null;
if($id)
{
$model = "tbl_$page"."s";
$view_arg = $model::whereId($id)->first();
// Error: Class 'tbl_posts' not found
//$view_arg = call_user_func(array($model, 'whereId'),$id)->first();
// Error : call_user_func() expects parameter 1 to be a valid callback, class 'tbl_posts' not found
}
// Some other codes...
}
我得到了错误:
错误:找不到类“tbl_posts”
在下一行:
$view_arg = $model::whereId($id)->first();
虽然以下代码工作正常:
tbl_posts::whereId($id)->first();
我还尝试了以下方法:
$view_arg = call_user_func(array($model, 'whereId'),$id)->first();
它给了我错误
错误:call_user_func() 期望参数 1 是有效的 回调,类
【问题讨论】:
-
tbl_posts在命名空间中吗? -
不,这是一个类(模型)
-
在字符串变量中传递模型调用的完整路径。
$model = App\Posts然后$view_arg = $model::whereId($id)->first(); -
要检查一下,你能显示
echo tbl_posts::class;给出的内容吗? -
你必须
useModel 类。