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:
<?php namespace app\controllers; use yii\web\Controller; class SiteController extends Controller { // ...现存的代码... public function actionSay($message = 'Hello') { return $this->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.
<?php use yii\helpers\Html; ?> <?= Html::encode($message) ?>
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

If you're asking "What is Yii?" check out my previous tutorial: Introduction to the Yii Framework, which reviews the benefits of Yii and outlines what's new in Yii 2.0, released in October 2014. Hmm> In this Programming with Yii2 series, I will guide readers in using the Yii2PHP framework. In today's tutorial, I will share with you how to leverage Yii's console functionality to run cron jobs. In the past, I've used wget - a web-accessible URL - in a cron job to run my background tasks. This raises security concerns and has some performance issues. While I discussed some ways to mitigate the risk in our Security for Startup series, I had hoped to transition to console-driven commands

With the rapid development of the Internet, APIs have become an important way to exchange data between various applications. Therefore, it has become increasingly important to develop an API framework that is easy to maintain, efficient, and stable. When choosing an API framework, Yii2 and Symfony are two popular choices among developers. So, which one is more suitable for API development? This article will compare these two frameworks and give some conclusions. 1. Basic introduction Yii2 and Symfony are mature PHP frameworks with corresponding extensions that can be used to develop

In modern software development, building a powerful content management system (CMS) is not an easy task. Not only do developers need to have extensive skills and experience, but they also need to use the most advanced technologies and tools to optimize their functionality and performance. This article introduces how to use Yii2 and GrapeJS, two popular open source software, to implement back-end CMS and front-end visual editing. Yii2 is a popular PHPWeb framework that provides rich tools and components to quickly build
