Yii2

How to disable Bootstrap in Yii2
How to disable Bootstrap in Yii2

By default, Yii2 comes bundled with the Bootstrap framework. After installing any configuration (basic or advanced), you’ll see a start page styled with Bootstrap. In this case (and possibly in all others), there’s nothing wrong with that, since it's just a placeholder, not a production website. But what if the client or studio decides to move away from such a popular solution like Bootstrap and opts for something like MaterializeCSS instead? In that case, you’ll need to disable Bootstrap and connect the MaterializeCSS framework (or any other you prefer).

The first thing we need to do is get rid of the built-in Bootstrap setup provided by Yii developers.

The second step is to disable Bootstrap support in the asset file (in basic, it’s located at /assets/AppAsset.php):

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yiiwebYiiAsset',
        'yiibootstrapBootstrapAsset',
    ];
}

Remove the line:

'yiibootstrapBootstrapAsset',

read more...

How to establish a connection to a database in the Yii2 framework
How to establish a connection to a database in the Yii2 framework

A simple and trivial task that a beginner Yii2 developer may encounter. Let's start with the fact that depending on the project configuration, the database can be set up in different ways. Let’s consider the two options provided by the framework:

  1. basic (simple setup)
  2. advanced (more complex setup)

In both cases, the database configuration (setting the prefix/tablePrefix, username/username, password/password, dsn, etc.) is done the same way. It all depends on the file location and the database connection class. In this example, we’ll look at how to connect to a MySQL database, as it's one of the most popular options.

In the basic template, the database configuration file can be found at: “/config/db.php”.

In the advanced template, things are a bit more complex (hence the name — for large-scale projects). There may be more than one configuration file (for backend, frontend, etc.). It all depends on how you initially structured your application. Since both admin and frontend parts usually work with the same database, I typically place the database configuration in the “/common/config/db.php” directory. Then I reference it in the “/common/config/main.php” config. That way, both backend and frontend will automatically inherit the database settings.

read more...

Caching in Yii2 and behaviors
Caching in Yii2 and behaviors

What should you do if you need to implement standard functionality (such as data caching) for some default models? In my case, behavior and its powerful capabilities came to the rescue.

In short, behavior in the Yii2 framework allows you to extend controllers and models (or anything inherited from the “Component” class) with additional methods. It works similarly to PHP traits but is not the same. If you’re interested in a more detailed explanation, check the official Yii documentation.

In this article, I’ll describe my experience combining caching, models, and behaviors.

Story: At a certain point in development, I needed to implement caching for four default models:

  • Rubrics
  • Categories
  • Regions
  • Cities

read more...

How to connect to multiple databases in Yii2
How to connect to multiple databases in Yii2

To connect to multiple databases in the Yii2 framework, you need to do the following:

1. Create two database configuration files in your site’s config. I keep the connections in separate files, and they look like this (example).

Connection to the first database:

return  [
	'class' => 'yiidbConnection',
	'dsn' => 'mysql:host=127.0.0.1;dbname=work_db1',
	'username' => 'root',
	'password' => '',
	'charset' => 'utf8',
	'tablePrefix' => 'tbl_',
];

Connection to the second database:

return  [
	'class' => 'yiidbConnection',
	'dsn' => 'mysql:host=127.0.0.1;dbname=work_db2',
	'username' => 'root',
	'password' => '',
	'charset' => 'utf8',
	'tablePrefix' => 'tbl_',
];

2. In the main config file, register both connections:

'db' => require(__DIR__ . '/db.php'),
'db2' => require(__DIR__ . '/db2.php'),

read more...