Home > PHP Framework > ThinkPHP > thinkphp how to call method in controller

thinkphp how to call method in controller

PHPz
Release: 2023-04-11 14:54:30
Original
1804 people have browsed it

ThinkPHP is a PHP framework based on the MVC framework, which provides us with very convenient and fast development tools. Among them, the controller is a very important component, which is responsible for business logic processing and data interaction. This article will introduce how to call methods in the controller to improve the scalability and maintainability of the program.

1. Create a controller

Before using a controller, we need to create a controller first. Suppose we want to create a UserController to handle the addition, deletion, modification, and query of user information. In ThinkPHP, we can create a UserController controller through the following command:

php think make:controller admin/UserController
Copy after login

This command will be used in the project Create a controller named UserController in the "application/admin/controller" directory. Next, we can perform relevant operations in the controller.

2. Calling the controller method

1. Basic call

After defining the method in the controller, we can call the method directly in the controller. As follows:

namespace app\admin\controller;
use think\Controller;
class UserController extends Controller{
    public function index(){
        return 'user index';
    }

    public function add(){
        return 'user add';
    }

    public function edit($id){
        return 'user edit '.$id;
    }

    public function delete($id){
        return 'user delete '.$id;
    }
}
Copy after login

By accessing routes "/admin/User/index", "/admin/User/add", "/admin/User/edit/1", "/admin/User/delete/1 "You can call the corresponding method in the UserController controller respectively.

2. Access other methods in the same controller

In the controller, we can call other methods in the same controller through "$this->Method Name()" . As follows:

namespace app\admin\controller;
use think\Controller;
class UserController extends Controller{
    public function index(){
        return $this->add();
    }

    public function add(){
        return 'user add';
    }
}
Copy after login

The add() method in the UserController controller can be called by accessing the route "/admin/User/index".

3. Access methods in other controllers

We can create a new controller through "controller('controller name')" and then use the methods in the controller. As follows:

namespace app\admin\controller;
use think\Controller;
class UserController extends Controller{
    public function index(){
        $test = controller('Test');
        return $test->index();
    }
}
Copy after login

In the above code, we created a new controller named Test and called its index() method.

4. Method call with parameters

If we want to pass parameters when calling a method, we can add "/parameter 1/parameter 2/.../parameter n" after the method name. Pass parameters. As follows:

namespace app\admin\controller;
use think\Controller;
class UserController extends Controller{
    public function edit($id){
        return 'user edit '.$id;
    }

    public function test(){
        return $this->edit(111);
    }
}
Copy after login

By accessing the route "/admin/User/test", you can call the edit($id) method in the UserController controller and pass parameter 111.

5. Access method of converting numbers and horizontal lines to camel case

In ThinkPHP, we can also automatically call the method of converting numbers and horizontal lines to camel case. As follows:

namespace app\admin\controller;
use think\Controller;
class UserController extends Controller{
    public function viewList(){
        return 'user view list';
    }

    public function userList(){
        return 'user list';
    }
}
Copy after login

By accessing the routes "/admin/User/view_list" and "/admin/User/user_list", you can call the viewList() method and userList() method in the UserController controller respectively.

3. Summary

In ThinkPHP, the controller is a very important component and plays a very critical role in the development, maintenance, and expansion of Web applications. In this article, we showed how to create a controller and call methods in the controller. Through these methods, we can effectively improve the scalability and maintainability of the program, further improving the development efficiency of the program.

The above is the detailed content of thinkphp how to call method in controller. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template