PHP

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...

How to Add Custom JavaScript Code to WordPress
How to Add Custom JavaScript Code to WordPress

While working on one of the WP projects, I needed to add some JS code. Normally, for such tasks, I use separate script files. However, in this case, the code was minimal, so I decided to place it directly into the HTML "body" of the document.
To do this, use the following PHP code:

add_action('wp_enqueue_scripts', function(){
	if(!wp_script_is( 'jquery', 'done' ))
	{
		wp_enqueue_script( 'jquery' );
	}
	wp_add_inline_script( 'jquery-migrate', 'alert("Hello")' );
});

Here's what we are doing:

read more...

How to get time of all timezones in PHP
How to get time of all timezones in PHP

To get an array of all time zones with their time offset from Greenwich, use the following function:

function get_time_timezones()
{
	$zones_array = array();
	$timestamp = time();
	
	$default_timezone = date_default_timezone_get();
	$timezone_list = timezone_identifiers_list();
	
	foreach ($timezone_list as $zone)
	{
		date_default_timezone_set($zone);
		$zones_array[$zone] = date('P', $timestamp);
	}
	
	date_default_timezone_set($default_timezone);
	
	return $zones_array;
}

read more...

How to generate a QR code for a website
How to generate a QR code for a website

In this short article, we’ll take a look at one way to generate a QR code using PHP.
According to the “brain” of our internet (Wikipedia), which rarely lies :), a QR code is:

QR code (Quick Response Code) — a trademark for a type of matrix barcode (or two-dimensional barcode), originally developed for the automotive industry in Japan.

To put it simply, a QR code is an image containing a square-shaped barcode. It can be scanned using a mobile phone (with the proper software installed) or a special scanning device.

Let’s get to the point

We won’t reinvent the wheel — to generate a QR code in PHP, we’ll use the ready-made library “phpqrcode” (thanks to the author!). The library can be downloaded from GitHub via this link.

The library is lightweight and consists of just a few dozen files totaling a little over 250 KB.

read more...