model里的代码:
/*
* 菜单列表
* @param int $pid 父级ID
* @author staitc7
*/
public function menuList(int $pid = 0): array {
$map = $pid === 0 ? ['pid' => 0] : ['pid' => $pid];
$map['status'] = ['neq', -1];
// $object = $this::all(function($query)use($map) {
// $query->where($map)->order('sort asc,id asc')->limit(10);
// });//闭包里使用分页是个坑 默认全表查询 不要用
$object = $this::where($map)->order('sort asc,id asc')->paginate(10);
return $object ? array_merge($object->toArray(),['page'=>$object->render()]): [];
}
控制器的代码如下 /*
* 菜单首页管理
* @param int $pid 父级ID
* @author staitc7
*/
public function index($pid = 0) {
$Menu = Loader::model('Menu');
$data = $Menu->menuList((int) $pid);
$value = [
'data' => $data['data'],
'page' => $data['page']
];
$view = new View([], Config::get('replace_str'));
$view->meta_title = '菜单列表';
return $view->assign($value)->fetch();
}
注意php版本。我是用的php7,php7应该成为框架里常态 最佳答案
