Current Location: Home> Latest Articles> Common Page Return Methods in ThinkPHP: Non-JS Solutions

Common Page Return Methods in ThinkPHP: Non-JS Solutions

gitbox 2025-06-29

Common Page Return Methods in ThinkPHP: Non-JS Solutions

ThinkPHP is a PHP development framework based on the MVC pattern, praised for its simple and efficient development style as well as its flexibility. In this article, we will explore various page return methods in ThinkPHP that do not rely on JS or other technologies.

Return HTML View Pages

Returning view pages in ThinkPHP is a common operation. You can either specify a view file directly or dynamically choose which view to load based on certain conditions.

Specify a View File

In the controller, specify the view file using a $view variable and call the view() method to return the corresponding view page.

pubic function index()
{
    $view = 'index';
    return view($view);
}

Dynamically Specify a View File

You can dynamically specify which view file to load based on input parameters, making this method more flexible than the previous one.

pubic function index()
{
    $view = input('view');
    return view($view);
}

Output Data in View Files

In your view files, you can output data using either native PHP syntax or ThinkPHP's template engine syntax.

// Output data example
<h1>{$title}</h1>

Return JSON Data

JSON is a lightweight data-interchange format widely used for data communication, especially in mobile and web applications. ThinkPHP supports returning JSON data in controllers.

pubic function index()
{
    $data = [
        'name' => 'John',
        'age' => 18,
        'sex' => 'Male'
    ];
    return json($data);
}

Use Template Engine to Return Data

Template engines provide a way to separate data from views, making views more flexible and maintainable. ThinkPHP allows you to use either native PHP syntax or the built-in template engine for data binding and rendering.

Native PHP Syntax

Using native PHP syntax is more flexible, but it may lead to maintenance challenges in larger projects.

pubic function index()
{
    $data = [
        'name' => 'John',
        'age' => 18,
        'sex' => 'Male'
    ];
    return $this->fetch('index', $data);
}

ThinkPHP Template Engine

ThinkPHP's template engine helps better separate data from views, making views easier to read and maintain.

pubic function index()
{
    $data = [
        'name' => 'John',
        'age' => 18,
        'sex' => 'Male'
    ];
    $this->assign($data);
    return $this->fetch('index');
}

Redirect to Another Page with Parameters

Page redirection is a common operation in web applications, and ThinkPHP offers a convenient way to redirect to another page while passing parameters.

pubic function login()
{
    $username = input('username');
    $password = input('password');
    if ($username == 'admin' && $password == '123456') {
        return redirect('index/index');
    } else {
        return redirect('login/index', ['msg' => 'Incorrect username or password']);
    }
}

Conclusion

This article introduced several common page return methods in ThinkPHP, including how to return HTML views, JSON data, use a template engine to render data, and how to implement page redirection with parameters. These methods will help developers better manage page rendering and data return in real-world projects.