Home  >  Article  >  Backend Development  >  thinkphp automatically completes session assignment in the model, thinkphpsession_PHP tutorial

thinkphp automatically completes session assignment in the model, thinkphpsession_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:34800browse

thinkphp automatically completes session assignment in the model, thinkphpsession

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

My thinkphp uses version 31, but it has automatic verification and field mapping. , autocomplete cannot be used if you follow the official steps

$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

How to determine session in thinkphp template

There are many ways to use them flexibly, such as
1. Call {$Think.session} in the template
2. Directly write php code in the tag to determine $_SESSION
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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/873920.htmlTechArticlethinkphp automatically completes session assignment in the model, thinkphpsession. I believe users who have used thinkphp know that thinkphp’s model can accomplish a lot Auxiliary functions, such as automatic verification, automatic completion...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn