How to Create Hello World Page in Yii 2

by Ahmed Khan  June 14, 2016

In the first installment, I introduced Yii2 and discussed several important aspects of the framework. In this second installment, I will discuss the creation of a Hello World page, and how to add it in Menu field. I will also create a page that will get a message from the browser and show it on that page. Let’s get started with the Hello World page first.

creating hello world page in yii2

Creating the Hello World in Yii2

Open your Yii2 directory and go to the views folder. Move to the site folder within it, create a new file and name it hello.php. Paste the following code in it.

Let’s understand the code:

  • Inside the php tag, I have first set the title of the page to Hello World.
  • Then comes the div class that presents the H1 heading.
  • Finally, the <p> tag with the ‘This is the hello page’ message.

Now that the Hello World page has been created successfully, it is time to call it in the SiteController. Go to the Controllers folder, open SiteController and create a new action, Hello in it .

In the actionHello function you will render the newly created hello page. Now head to the browser and open Yii2 “basic/web” page and  include “/index.php?r=site/hello” in it. The complete URL will be:  “basic/web /index.php?r=site/hello”. When the page loads, the Hello World page would be visible.

Hello World Page

Now that the  page has been created, add the page to the menu bar. For this, go to the views folder and then to layouts folder and open main.php. In the file, under echo Nav::widget, add this: “[‘label’ => ‘Hello’, ‘url’ => [‘/site/hello’]],” just above Yii::$app->user->isGuest ?.  Save the file.

echo Nav

Now reload the URL (or refresh the page, if it is still open) and you will see that a Hello is added to the menu bar.

Hellow World Page in Yii2

We have successfully created a hello world page. Now let’s create a page which will get a message from the browser and show it on that page.

Showing Message in a Page From Browser

Now go to the views folder, and then into the site folder. Create a new file and name it hello2.php. Now paste the following code in it.

HTML is a Yii2 helper class which helps you in generating commonly used HTML tags easily. I will use the encode() function which will encode the incoming message (from the controller) in HTML. Now call this in the controller. Head to the Controllers folder and open SiteController. Paste the following code:

We have created a new action, Hello2, with a variable named $message which will come from the browser. Inside the function, I have called the newly created page hello2 in render and have also passed the variable $message to an array. Now let’s call this action in browser by ?r=site/hello2 and send message along with it by add &message=My+First+Browser+Message.

My First Browser Message

Summary

In this tutorial I discussed the creation of a Hello World page, and how to get a variable inside view and show the message in the view. In the upcoming installment of this series, I will use Yii2 framework to create a comprehensive contact form app. Here is the complete list of topics covered in this series on Yii2. If you find any mistakes in the article, feel free to highlight the problem in the comments section.

To begin work on Yii2, launch your free trial on the Cloudways Managed PHP Hosting Platform. Designed to work flawlessly with PHP, the Thunderstack formula ensures blazing speeds for your web apps.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...