thinkphp 代码复用的问题
代言
代言 2017-07-01 09:11:50
[PHP讨论组]

用ThinkPhp3.2做开发,因为很多时候要用到增删改查操作,为了增加代码的复用。我在common中写了一个curdControler和curdModel做代码的增删改查,需要用到增删改查时直接继承curdController和curdModel。

现在有一个问题一般curd操作都要做权限的判断,否则会很危险。我这里的思路是在curdController构造方法中调用一个checkAuth();因为各种功能,权限控制的方法会有不同,怎么强制继承curdController的子类必须重载checkAuth方法了?

我的想法是,我把权限判断函数 定义为抽象方法

protected abstract function checkAuth()

类curdController定义为抽象类,但是抽象类不能被实例化那么构造函数的代码是不是就无效了,这样实现有什么不妥

第二问题,大家在做tp的代码重用时,有什么更好的思路,我这种做法有什么隐患和问题了,谢谢指教.

class CurdController extends Controller
{
    //基础curd类必须进行权限判断,否则会造成很大的问题
    public function __construct()
    {
        parent::__construct();
        $this->checkAuth();
    }

    //存储模型对象
    protected  $_model;
    //权限判断函数
    protected function checkAuth(){}
    //列表处理函数
    public function listC(){
//      列表前置函数
        $this->beforeList();
        $data=$this->_model->lists();
        $this->assign('lists',$data);
        $this->display();
    }

    public function delC(){
        $id=intval(I('get.id'));
        $where['id']=$id;
        $res=$this->_model->del($where);
        $this->redirectUrl($res,'listC');
    }

    public function addC(){
//        添加前置函数
        $this->beforeAdd();
        if(IS_POST){
            $data=I('post.');
            $res=$this->_model->Store($data);
            $this->redirectUrl($res,'listC');
        }
        $this->display();
    }

    public function editC(){
        $id=intval(I('get.id'));
        //where的数组形式
        $where['id']=$id;
//        编辑前置函数
        $this->beforeEdit($where);
        if(IS_POST){
            $where=I('post.');
            $where['id']=$id;
            $res=$this->_model->Store($where);
            $this->redirectUrl($res,'listC');
        }
        $this->display();
    }
    //列表前置操作
    protected function beforeList(){

    }
    /**
     * 添加控制器前置操作
     */
    protected function beforeAdd(){

    }

    /**
     * 编辑控制器前置操作
     * @param $where
     */
    protected function beforeEdit($where){

    }
代言
代言

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号