Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to compile the Model function. The common method is to create a corresponding Model in the module \ Model directory. class. PHP file to implement the Model function.
If general functions are available, you can create a corresponding Model in the Common \ Model to implement a general Model through inheritance.
In this method, the file structure is clear, but each corresponding Model requires file creation to inherit and implement, and the M method is not powerful.
I do not like this structure. Obviously, by modifying the core load code, you can implement a single-file general Model without creating so many Model files that only inherit the code.
First, we declare that if the modified Code causes exceptions or causes other problems, we recommend that you use the original version by private message or continue to use the original version. In this case, we will not be liable for any losses incurred.
Find the function D function of ThinkPHP \ Common \ functions. php.
Find
if(empty($name)) return new Think\Model;
Change
$parent = class_exists('\\Common\\Model') ? '\\Common\\Model' : '\\Think\\Model';
if(empty($name)) return new $parent;
Find
$model = class_exists($class)? new $class($name) : new Think\Model($name);
Change
$model = class_exists($class)? new $class($name) : new $parent($name);
Find
$model = new Think\Model(basename($name));
Change
$model = new $parent(basename($name));
Find the function M function in the PHP file.
Find
$class = 'Think\\Model';
Change
$class = class_exists('Common\\Model') ? 'Common\\Model' : 'Think\\Model';
The implementation principle is to inherit the Common \ Model to implement a single-file general Model.
Of course, both the D and M methods can be inherited from the Common \ Model after the above modifications.
Create the Application \ Common \ Model. php file,
File template. Remember that the encoding format UTF8 has no signature.
Namespace Common;
Class Model extends \ Think \ Model {
Public function test () {// test Method
Echo ('common \ Model test ');
}
}
The reason for not using use Think \ Model above is to avoid repeated conflicts in the current Common \ Model class declaration, and to make the inheritance class better write the Model almost the same as the native Model. For the advantages, see the following code.
If you need to use Common \ Model in the Model of the current Module
The file template is
Namespace Home \ Model;
Use Common \ Model;
Class XxxModel extends Model {
Public function test () {// test Method
Parent: test (); // call the Common \ Model: test () method
Echo ('home \ Model test ');
}
}
The implementation of the single-file Universal Controller is relatively complicated, and the stability is also being tested.
It is also very convenient for general-purpose operations such as logon, password modification, and so on.
I spoke about the official code release function, clicked preview, and disabled it. I found that the preview was not _ blank, and the result was revoked, as a result, it is rewritten.
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB