Home  >  Article  >  PHP Framework  >  Controllers in Yii framework: handling requests

Controllers in Yii framework: handling requests

WBOY
WBOYOriginal
2023-06-21 10:32:481595browse

The Yii framework is a development framework based on the PHP language. It provides developers with many practical tools and functions, such as data table operations, request processing, page rendering, etc. The controller is a very important part of the Yii framework. This article will introduce the controller in the Yii framework.

What is a controller?

In the Yii framework, a controller is a class used to handle requests. It is mainly responsible for forwarding requests sent by users to corresponding processing methods (Action), and generating corresponding response information through these methods. The controller plays the role of "controller" in the MVC (Model-View-Controller) design pattern and is used to control the behavior of the program.

How to create a controller?

In the Yii framework, we can create controllers through Gii tools or manual creation. The manual creation method refers to creating a new PHP class file inherited from the Yii framework base class in the controller directory, and defining some processing methods in the class, which correspond to the operations that need to be performed after the user requests.

The following is a simple example:

Create a controller file named SiteController and save it in the controllers directory. The SiteController class inherits from the base class Controller, which contains two Action processing methods: actionIndex and actionAbout.

<?php

namespace appcontrollers;

use yiiwebController;

class SiteController extends Controller
{
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionAbout()
    {
        return $this->render('about');
    }
}

In the above code, we defined two action methods in SiteController, which handle access requests for /index and /about respectively.

How to call the controller?

In the Yii framework, we can access the Action method in the controller through the URL. For example, if we want to access the actionIndex method in SiteController, we can enter the following URL in the browser:

http://localhost/index.php?r=site/index

Among them, the r parameter represents the route, and site/index corresponds to the actionIndex method in SiteController. In this way, we can call methods in the controller.

Commonly used methods in controllers

In controllers, we can use many methods predefined in the Yii framework to achieve various functions. Here are some commonly used controller methods:

  1. render($view, $params = []) - Renders a view file and returns the result to the user. The $view parameter represents the view file name to be rendered. The $params parameter is an optional array that stores the data that needs to be used in the view;
  2. redirect($url, $statusCode = 302) - Redirect user requests. The $url parameter specifies the redirected URL, and the $statusCode parameter is the HTTP status code;
  3. goBack($defaultUrl = null) - Return to the previous page. The $defaultUrl parameter specifies the default returned URL;
  4. createUrl($route, $params = [], $ampersand = '&') - Create a complete URL. The $route parameter specifies the Action method to be accessed. The $params parameter is an optional array that stores the data required for the request. The $ampersand parameter specifies the connector in the URL;
  5. redirect($ url, $statusCode = 302) - Redirect user request. The $url parameter specifies the redirected URL, and the $statusCode parameter is the HTTP status code;
  6. isAjax - determines whether it is an Ajax request.

Conclusion

The controller is a very important part of the Yii framework and is often used in actual development work. This article introduces the controller in the Yii framework, and explains the creation, calling and common methods of the controller. I hope this article can help readers better understand and apply controllers in the Yii framework.

The above is the detailed content of Controllers in Yii framework: handling requests. 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