Book Image

Yii2 Application Development Cookbook - Third Edition

By : Sergey Ivanov, Andrew Bogdanov, Dmitry Eliseev
Book Image

Yii2 Application Development Cookbook - Third Edition

By: Sergey Ivanov, Andrew Bogdanov, Dmitry Eliseev

Overview of this book

Yii is a free, open source web application development framework written in PHP5 that promotes clean DRY design and encourages rapid development. It works to streamline your application development time and helps to ensure an extremely efficient, extensible, and maintainable end product. Being extremely performance optimized, Yii is a perfect choice for any size project. However, it has been built with sophisticated, enterprise applications in mind. You have full control over the configuration from head-to-toe (presentation-to-persistence) to conform to your enterprise development guidelines. It comes packaged with tools to help test and debug your application, and has clear and comprehensive documentation. This book is a collection of Yii2 recipes. Each recipe is represented as a full and independent item, which showcases solutions from real web-applications. So you can easily reproduce them in your environment and learn Yii2 fast and without tears. All recipes are explained with step-by-step code examples and clear screenshots. Yii2 is like a suit that looks great off the rack, but is also very easy to tailor to fit your needs. Virtually every component of the framework is extensible. This book will show how to use official extensions, extend any component, or write a new one. This book will help you create modern web applications quickly, and make sure they perform well using examples and business logic from real life. You will deal with the Yii command line, migrations, and assets. You will learn about role-based access, security, and deployment. We’ll show you how to easily get started, configure your environment, and be ready to write web applications efficiently and quickly.
Table of Contents (19 chapters)
Yii2 Application Development Cookbook Third Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
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\webApplication)

  • 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