Home  >  Article  >  PHP Framework  >  How to create hello page using yii framework

How to create hello page using yii framework

(*-*)浩
(*-*)浩Original
2019-11-26 14:44:062711browse

How to create hello page using yii framework

This chapter describes how to create a new “Hello” page in your application. To achieve this goal, an action and a view will be created:

The application will dispatch the page request to the action (Recommended learning: yii tutorial)

The action will in turn render the view presenting "Hello" to the end user

Throughout this chapter, you will learn three things:

How to create an action to respond to requests,

how to create a view to construct response content,

and how an application dispatches requests to actions.

Create action

For "Hello", you need to create a say action that receives the message parameter from the request and displays it to the end user. If the request does not provide a message parameter, the operation will display the default parameter "Hello".

信息: 操作是最终用户可以直接访问并执行的对象。 操作被组织在控制器中。 一个操作的执行结果就是最终用户收到的响应内容。

The operation must be declared in the controller. For simplicity, you can declare the say action directly in the SiteController controller. This controller is defined by the file controllers/SiteController.php. The following is the declaration of an action:

render('say', ['message' => $message]);
    }
}

In the above SiteController code, the say action is defined as the actionSay method. Yii uses the action prefix to distinguish between ordinary methods and operations. The name following the action prefix is ​​mapped to the ID of the action.

When it comes to naming operations, you should understand how Yii handles operation IDs. Operation IDs are always treated in lowercase. If an operation ID consists of multiple words, the words will be connected by hyphens (such as create-comment).

When the operation ID is mapped to the method name, hyphens are removed, the first letter of each word is capitalized, and the action prefix is ​​added. Example: The action ID create-comment is equivalent to the method name actionCreateComment.

The operation method in the above code accepts a parameter $message, whose default value is "Hello" (just like you set the default value of other functions or methods in PHP). When the application receives the request and determines that the say operation will respond to the request, the application will find the corresponding value from the request parameters and pass it in.

In other words, if the request contains a message parameter and its value is "Goodbye", the $message variable in the action method will also be filled with "Goodbye".

In the operation method, render() is used to render a view file named say. The message parameter is also passed into the view so it can be used inside. The action method returns the rendering result. The results are received by the application and displayed to the end user's browser (as part of the full page HTML).

Creating Views

Views are scripts you use to generate response content. To say "Hello", you need to create a say view that displays the message parameter passed from the action method.


say views should be saved as views/site/say.php file. When the render() method is called in an operation, it will load the PHP file according to the views/controller ID/view name.php path.

Note that in the above code, the message parameter is processed by the HTML-encoded method before output. This is necessary because when the parameters come from the end user, malicious JavaScript code that may be hidden in the parameters can lead to cross-site scripting (XSS) attacks.

Of course, you will probably put more content in the say view. Content can consist of HTML tags, plain text, or even PHP statements. In fact, the say view is a PHP script executed by render(). The content output by the view script will be returned to the application as a response result. The application will in turn output the results to the end user.

Trial Run

After creating the action and view, you can access the new page through the following URL:

http://hostname/index.php?r=site/say&message=Hello+World

How to create hello page using yii framework

This URL will output a page containing "Hello World", using the same header and trailer as other pages in the application.

If you omit the message parameter in the URL, you will see that the page only displays "Hello". This is because message is passed as a parameter to the actionSay() method, and when it is omitted, the default "Hello" parameter will be used instead.

信息: 新页面和其它页面使用同样的头部和尾部是因为 render() 方法会自动把 say 视图执行的结果嵌入称为布局的文件中, 本例中是 views/layouts/main.php。

The parameter r in the URL above needs more explanation. It represents a route, which is an independent ID at the entire application level that points to a specific operation. The routing format is controller ID/operation ID. When the application accepts a request, it checks the parameters and uses the controller ID to determine which controller should be used to handle the request. The corresponding controller will then use the action ID to determine which action method will be used to do the specific work.

In the above example, the route site/say will be parsed to the SiteController controller and the say operation therein. Therefore the SiteController::actionSay() method will be called to handle the request.

信息: 与操作一样,一个应用中控制器同样有唯一的 ID。 控制器 ID 和操作 ID 使用同样的命名规则。 控制器的类名源自于控制器 ID, 移除了连字符,每个单词首字母大写,并加上 Controller 后缀。 例子:控制器 ID post-comment 相当于控制器类名 PostCommentController

The above is the detailed content of How to create hello page using yii framework. 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