Use ThinkPhp3.2 for development, because many times you need to use addition, deletion, modification, and search operations in order to increase code reuse. I wrote a curdControler and curdModel in common to do the addition, deletion, modification and query of the code. When I need to use the addition, deletion, modification and query, I directly inherit curdController and curdModel.
Now there is a problem. Generally, curd operation requires permission judgment, otherwise it will be very dangerous. My idea here is to call a checkAuth() in the curdController construction method; because of various functions, the permission control methods will be different. How to force subclasses that inherit curdController to overload the checkAuth method?
My idea is that I define the permission judgment function as an abstract method
protected abstract function checkAuth()
The class curdController is defined as an abstract class, but if the abstract class cannot be instantiated, then the code of the constructor will be invalid. What is wrong with this implementation?
Second question, do you have any better ideas when reusing TP code? What are the hidden dangers and problems of my approach? Thank you for your advice.
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){
}
For code reuse, I recommend using PHP features:
http://php.net/manual/zh/lang...
Or use closure binding (not recommended):
http://php.net /manual/en/clos...
checkAuth
You can write different traits through different businesses, and use the corresponding traits in the class that specifically inherits curdController. SincecheckAuth()
only returns the true or false of the verification result, this can be applied to any Controller CustomizecheckAuth()
.In response to your first question, since you manually called
parent::__construct();
in the constructor of the subclass that inherited the abstract classcurdController
, as long as the subclass is instantiated, the construction of the parent class Functions can also be used, see the example below:Code:
Result:
In response to your second question, I personally feel that the entire structural framework is a bit rough. Common one-to-many and many-to-many relationships still need to be done manually. It is recommended to encapsulate this association operation.
Although the framework I personally use more often is
CodeIgniter
, I think the basic ideas of the MVC (HMVC) model are the same, so let’s talk about the reuse encapsulation I personally made inCodeIgniter
:I personally put the atomic operations of the data table on the bottom layer
Model
(based onCI-Base-Model
, you can take a look athas_many
andbelongs_to
inCI-Base-Model
Configuration), and I inheritedCI-Base-Model
and wrote aCRUD_Model
myself. In thisCRUD_Model
, almost relying on some configuration items and some rewriting, I can quickly generate a standard CRUD array, which can be released below Part of the source code:I hope the idea of my model can help you refine CRUD logic.