Author: Awais Farooq
-
Views
Views are responsible for presenting the data to end users. In web applications, Views are just PHP script files containing HTML and PHP code. Creating Views Step 1 − Let us have a look at the ‘About’ view of the basic application template. The $this variable refers to the view component that manages and renders this view template. This is how the ‘About’ page…
-
Modules
A module is an entity that has its own models, views, controllers, and possibly other modules. It is practically an application inside the application. Step 1 − Create a folder called modules inside your project root. Inside the modules folder, create a folder named hello. This will be the basic folder for our Hello module. Step 2 − Inside the hello folder,…
-
Widgets
A widget is a reusable client-side code, which contains HTML, CSS, and JS. This code includes minimal logic and is wrapped in a yii\base\Widget object. We can easily insert and apply this object in any view. Step 1 − To see widgets in action, create an actionTestWidget function in the SiteController with the following code. In the above example, we just returned…
-
Models
Models are objects representing business logic and rules. To create a model, you should extend the yii\base\Model class or its subclasses. Attributes Attributes represent the business data. They can be accessed like array elements or object properties. Each attribute is a publicly accessible property of a model. To specify what attributes a model possesses, you should override…
-
Using Actions
To create an action in a controller class, you should define a public method whose name starts with the word action. The return data of an action represents the response to be sent to the end user. Step 1 − Let us define the hello-world action in our ExampleController. Step 2 − Type http://localhost:8080/index.php?r=example/hello-world in the address bar of the…
-
Using Controllers
Controllers in web applications should extend from yii\web\Controller or its child classes. In console applications, they should extend from yii\console\Controller or its child classes. Let us create an example controller in the controllers folder. Step 1 − Inside the Controllers folder, create a file called ExampleController.php with the following code. Step 2 − Create an example view in the views/example folder. Inside that folder, create a file…
-
Controllers
Controllers are responsible for processing requests and generating responses. After user’s request, the controller will analyze request data, pass them to model, then insert the model result into a view, and generate a response. Understanding Actions Controllers include actions. They are the basic units that user can request for execution. A controller can have one…
-
Entry Scripts
Entry scripts are responsible for starting a request handling cycle. They are just PHP scripts accessible by users. The following illustration shows the structure of an application − Web application (as well as console application) has a single entry script. The End user makes request to the entry script. Then the entry script instantiates application…
-
Application Structure
There is only one folder in the overall code base that is publicly available for the web server. It is the web directory. Other folders outside the web root directory are out of reach for the web server. Note − All project dependencies are located in the composer.json file. Yii2 has a few important packages that are already…
-
Create Page
Now we are going to create a “Hello world” page in your application. To create a page, we must create an action and a view. Actions are declared in controllers. The end user will receive the execution result of an action. Step 1 − Declare the speak action in the existing SiteController, which is defined in the class file…