I believe users who have used thinkphp know that thinkphp’s model can complete many auxiliary functions, such as
such as Automatic verification, automatic completion, etc. Today in development, I encountered the function of getting the session value
and then automatically assigning it in the automatic completion. Please see the code for details;
class ArticlelModel extends Model {
protected $_auto = array (
array('addtime','time',1,'function'),
array('username','getName',1,'callback')
);
//This function gets the name value in the session
protected function getName(){
return $_SESSION["name"];
}
}
Required here Pay attention to the difference between the last parameter function and callback;
function: when using a function, it will automatically go to Common/common.php to find the corresponding function;
callback: use the callback method defined in the current model
More programming video tutorials can be downloaded at: http://www.jscto.net
$User=new Model('Admin'); //The problem lies in this line of code. The Model() or M() method instantiates the basic model, and you add automatic validation to the model, which is a custom model. Therefore the automatic verification part will not work anymore. // Modify the previous sentence as follows $User=new AdminModel(); // Directly instantiate it as your custom model // Or simpler $User=D('Admin'); // For details, refer to the official documentation 6.2 Model instantiation
There are many ways to use them flexibly, such as
1. Call {$Think.session} in the template
2. Directly write php code in the
3. Write in the template, and then write PHP code directly in it to determine $_SESSION
4. In Action, assign the $_SESSION variable to the template through the assign() method
5. In the custom function library, create a new function to return the $_SESSION variable, and then use {$variable name | function name} in the template file to assign the $_SESSION returned by the function to a variable in the template
The above five methods are all available, you can also think of your own method.