Home  >  Article  >  PHP Framework  >  Detailed explanation of how thinkPHP calls views

Detailed explanation of how thinkPHP calls views

PHPz
PHPzOriginal
2023-04-11 15:09:421205browse

ThinkPHP is an MVC framework based on PHP. It uses the MVC design pattern to separate business logic and data operations, making development and maintenance easier. In ThinkPHP, the view is the View layer in MVC, which is used to display data to users and is decoupled from the controller (Controller) and the model (Model). Below we will learn in detail how to call the view.

1. Create View

In ThinkPHP, we can call the view (View) through the controller (Controller), but before creating the view, we need to create a controller first.

The creation method of the controller is as follows:

<?php
namespace app\controller;
use think\Controller;
class Index extends Controller{
    public function index(){

    }
}

The function of the controller is to receive user requests, process user requests, and decouple from the model (Model) and the view (View) to achieve a single responsibility in principle.

Next, we need to call the view in the controller.

In ThinkPHP, views are stored in the /application/view/ directory. You can create new folders and files in the directory as needed. For example, we create a new index.html file in the /application/view/ directory:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ThinkPHP调用视图示例</title>
</head>
<body>
    <h1>欢迎使用ThinkPHP!</h1>
</body>
</html>

In the controller, we can call View The fetch method of the class is used to render the view.

<?php
namespace app\controller;
use think\Controller;
class Index extends Controller{
    public function index(){
        return $this->fetch('index');
    }
}

In the above code, we call the view through the controllerindex.html,$this->fetch('index')returns the view's HTML code, ultimately used to render the page.

If we place the index.html file in the /application/view/index/ directory, we can call the view in the controller like this:

return $this->fetch('index/index');

2. View parameter passing

Sometimes, we need to dynamically display data in the view. In ThinkPHP, we can pass data to the view by passing parameters.

In the controller, we can use the assign method to pass data.

<?php
namespace app\controller;
use think\Controller;
class Index extends Controller{
    public function index(){
        $name = &#39;ThinkPHP&#39;;
        $this->assign('name',$name);
        return $this->fetch('index');
    }
}

In the above code, we assign the variable $name to ThinkPHP and pass it to the view.

In the view, we can display the passed data in the form of {$name}.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ThinkPHP调用视图示例</title>
</head>
<body>
    <h1>欢迎使用{$name}!</h1>
</body>
</html>

3. Get POST data in the view

Sometimes, we need to submit form data in the view and pass it to the controller for processing. In ThinkPHP, we can access POST data through the request method.

In the view, we can create a form and submit data to the controller through POST.

<form method="post" action="<?php echo url(&#39;index/add&#39;); ?>">
    <input type="text" name="username" placeholder="请输入用户名">
    <input type="text" name="password" placeholder="请输入密码">
    <button type="submit">提交</button>
</form>

In the controller, we can obtain POST data through the request method.

<?php
namespace app\controller;
use think\Controller;
class Index extends Controller{
    public function add(){
        $username = $this->request->post('username');
        $password = $this->request->post('password');
        //执行具体的业务逻辑
    }
}

In the above code, we use the $this->request->post() method to get the POST data.

Through the above method, we can easily call the view and decouple it from the controller to achieve the goal of the MVC architecture.

The above is the detailed content of Detailed explanation of how thinkPHP calls views. For more information, please follow other related articles on the PHP Chinese website!

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