Home >PHP Framework >ThinkPHP >Method call in thinkphp
ThinkPHP is an excellent PHP development framework that provides many convenient functions for method calling. This article will introduce you to some of the method calling methods.
1. Controller method call
In ThinkPHP, the controller is one of the core codes in the MVC framework. There are many methods available in the controller. We can define a method in the controller as follows:
public function index(){ echo '这是控制器中的index方法'; }
Then we can enter the URL in the browser to call this method:
http://localhost/tp5/index.php/index/index
Among them, index.php is the ThinkPHP framework Entry file, index is the controller name, index is the method name of the controller. What needs to be noted here is that in the ThinkPHP5 framework, due to the existence of routing, the above URL can be simplified to:
http://localhost/tp5/public/index/index
Among them, tp5 is the name of the root directory of the project, and public is the name of the entry directory of the project.
2. Model method call
In ThinkPHP, the model is the bridge between the controller and the database. In the model, we can define many common methods to operate the database, such as addition, deletion, modification, query, etc. We can define a method in the model as follows:
public function getList(){ $list = $this -> select(); return $list; }
This method can be used to query records in the database and return an array. We can call this method in the controller to get the query results:
public function index(){ $model = new DemoModel(); $list = $model -> getList(); $this -> assign('list', $list); return $this -> fetch(); }
After the query is successful, we assign the results to the template, and then use the fetch() method to display the query results, so we can Use list variables to display query results.
3. Assistant function call
In ThinkPHP, the assistant function is a set of simple functions that can be used to handle common operations in the project. These functions are defined in the file helper.php, and we can directly call these functions to complete some operations. For example:
$data = [ 'name' => 'ThinkPHP', 'email' => 'thinkphp@qq.com', ]; dump($data);
This example uses the dump() function to display the contents of the $data array. The dump() function is a very useful helper function that can be used to print the structure and content of data.
4. TP built-in method calling
In addition to the above usage, ThinkPHP also provides some other method calling methods, such as:
In ThinkPHP, we can use the Log class to record system logs. For example:
Log::record('这是一条系统日志');
This method will write a record to the system log file.
In ThinkPHP, we can use the Cache class to implement the caching function. For example:
Cache::set('name', 'Tom', 3600);
This method can cache data into Cache and set the validity period to 3600 seconds.
3. Summary
Through the above introduction, we can see that ThinkPHP provides many convenient method calling methods, which can help us better complete the operations in the project. We must be proficient in the use of these methods in order to better utilize the ThinkPHP framework for development.
The above is the detailed content of Method call in thinkphp. For more information, please follow other related articles on the PHP Chinese website!