Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Yii2 Application Development Cookbook

You're reading from   Yii2 Application Development Cookbook Discover 100 useful recipes that will bring the best out of the Yii2 framework and be on the bleeding edge of web development today

Arrow left icon
Product type Paperback
Published in Nov 2016
Publisher
ISBN-13 9781785281761
Length 584 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Dmitry Eliseev Dmitry Eliseev
Author Profile Icon Dmitry Eliseev
Dmitry Eliseev
Andrew Bogdanov Andrew Bogdanov
Author Profile Icon Andrew Bogdanov
Andrew Bogdanov
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Fundamentals FREE CHAPTER 2. Routing, Controllers, and Views 3. ActiveRecord, Model, and Database 4. Forms 5. Security 6. RESTful Web Services 7. Official Extensions 8. Extending Yii 9. Performance Tuning 10. Deployment 11. Testing 12. Debugging, Logging, and Error Handling Index

Configuring components

Yii is a very customizable framework. Moreover, as in all customizable code, there should be a convenient way to set up different application parts. In Yii, this is provided through configuration files located at config.

Getting ready

Create a new application by using the Composer package manager as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-startinstallation.html.

How to do it…

If you have worked with Yii before, then you have probably configured a database connection:

return [
    …
    'components' => [
        'db' => [
            'class' => 'system.db.CDbConnection',
            'dsn' => 'mysql:host=localhost;dbname=database_name',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
        …
    ],
    …
];

This way of configuring components is used when you want to use a component across all application parts. With the preceding configuration, you can access a component by its name, such as Yii::$app->db.

How it works…

When you are using the Yii::$app->db component for the first time directly or through an Active Record model, Yii creates a component and initializes its public properties with the corresponding values provided in db array under the components section of the application configuration file. In the preceding code, dsn value will be assigned to yii\db\Connection::dsn, username will be assigned to Connection::username, and so on.

If you want to find out what charset stands for or want to know what else you can configure in the db component, then you need to know its class. In the case of the db component, the class is yii\db\Connection. You can just open the class and look for its public properties, which you can set from config.

In the preceding code, the class property is a bit special because it is used to specify the component class name. It does not exist in the yii\db\Connection class. Therefore, it can be used to override a class as follows:

return [
    …
    'components' => [
        'db' => [
            'class' => app\components\MyConnection',
            …
        ],
        …
    ],
     …
);

This way, you can override each application component; this is very useful whenever a standard component does not fit your application.

Built-in components

Now, let's find out which standard Yii application components you can configure. There are two application types bundled with Yii:

  • Web application (yii\web\Application)
  • Console application (yii\console\Application)

Both are extended from yii\base\Application, so both console and web applications share its components.

You can get the component names from the source code of the coreComponents() application's method.

You can add your own application components (classes extended from yii\base\Component) by simply adding new configuration items and pointing their class properties to your custom classes.

See also

You have been reading a chapter from
Yii2 Application Development Cookbook - Third Edition
Published in: Nov 2016
Publisher:
ISBN-13: 9781785281761
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image