|

Getting Started With The Yii Application Instance

Getting Started With The Yii Application Instance

A key idea to understand in Yii is that of the application. When we think of the word application, it could mean any number of things. In the Yii sense, it is the object that controls the structure and lifecyle of requests in your website. There are web applications, in addition to console applications, however we’ll be taking a look at the web variety in this episode. When a request is made of a Yii powered web application, it must enter through an entry script. In this entry script Yii will configure the application via $application = new yiiwebApplication($config); and then run it via $application->run(); An instance of this object is now available everywhere in your website, and you can access it via Yii::$app. We’re going to take a look at some of the common ways to access this instance.


Yii::$app->request

First up is the web request class which deals with all manner of HTTP requests in the application, much like when a request is sent to a route in yii. When you think of things like $_SERVER, $_POST, $_GET, $_COOKIES, and REST, in PHP, you can think of Yii::$app->request as Yii’s representation of this. When you look at the documentation, you’ll see there are 52 properties and 89 public methods in this class only! So we can see, there is a bit of a learning curve with Yii because the classes do so much. We will not cover each of these, we just want to look at some common ones to see how they work, then it’s up to you to learn the rest on your own as needed.

Yii::$app->request->post()

The documentation tells us that this returns the POST parameter with a given name. If name isn’t specified, returns an array of all POST parameters. Let’s test it out, if we want to access the post request of the login form of the advanced Yii Application we can print_r to the screen.

Yii::$app->request->post()

Typically this is used to populate the model like this

Learn More


Yii::$app->formatter

This class provides all kinds of commonly used data formatting methods for dates, times, languages, and so on. Common uses would be things like

Learn More


Yii::$app->user

This class handles the management of users in your application and their authentication status. The basic and advanced templates give you an instance of this right out of the box. Common uses would be things like

Learn More


Yii::$app->getSecurity

This is used for password hashing and verification, generating semi random data, working with data integrity, and encryption. A common use might be something like

Learn More


Yii::$app->mailer

Most websites or applications will have the need to send mail at some point and this is how to access mail functionality in Yii. Note that by default, Yii uses swiftmailer, however you can change this out in the configuration of your components for the mailer class. An example would be something like

Learn More


Yii::$app->response

When your server sends back a response to the client, it has headers, cookies, and content. All of this can be accessed in this class. Some use examples would be things like

Learn More


Yii::$app->db

This class represents the database connection object of your application. You can use it to get and work with any database property you can think of. Essentially it is a Yii specific implementation of the PHP PDO. If you are creating models that extend Active Record, it may make sense to just go with that approach instead. If you do need direct access however in the absence of Active Record models you can do so with this approach. An example query might be

Learn More


Yii::$app->urlManager

This is the class which will handle parsing of the http requests to your application, in addition to setting up pretty urls, and generating links in your application. Most of the magic is handled behind the scenes however you can use things like the following if you like


Yii::$app->authManager

This class deals with configuring and using Role Based Access Control in you application. You can add roles to the rbac system, assign roles to users, check permissions, and more.

Learn More


Yii::$app->view

Represents a view object in the model view controller design pattern. View contains many methods to work with the view object however the most important of these is the render() method which is what creates the actual view output in your application. View Events may also prove useful for various scenarios.

Learn More

Conclusion

This post should provide a good idea of what the the application instance is in the Yii Framework. We learned that the application instance is created when a request comes in to the site via an entry script. Once the application instance is created, we can access a huge number of properties and methods via Yii::$app anywhere in the site. We only took a quick look at some of the more common ones here, but do follow the links out to the other resources to learn more.

|