Yii

Yii2 Internationalization (i18n) for advanced template

We are working on a project that makes use of Yii2 advanced template. This project needs to support Internationalization (i18n). In each of our application, we have a translation configuration. This is needed to generate the translation. The files would be generated in backend/messages, common/messages or frontend/messages. Here’s a sample. <?php // backend/config/i18n.php // common/config/i18n.php // frontend/config/i18n.php return [ 'sourcePath' => __DIR__ . DIRECTORY_SEPARATOR . '..', 'languages' => [ 'zh-CN', ], 'translator' => 'Yii::t', 'sort' => true, 'removeUnused' => true, 'markUnused' => true, 'only' => ['*.php'], 'except' => [ '.svn', '.git', '.gitignore', '.gitkeep', '.hgignore', '.hgkeep', '/assets', '/command', '/config', '/mail', '/messages', '/modules', '/runtime', '/tests', '/vagrant', '/vendor', '/views/user/settings', '/web', '/widgets', ], 'format' => 'php', 'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'messages', 'overwrite' => true, ]; Here are some samples of how the text should be wrapped. ...

<span title='2023-05-12 13:00:00 +0800 +0800'>May 12, 2023</span>
Yii

Yii 2 Codeception Fixtures Gotcha

We use MongoDB (important point) with our Yii 2 applicaton. While setting up our API test fixtures in our Cest file, $I->grabFixture was not able to find the data we wanted eventhough we have defined it within the dataFile. [yii\base\ErrorException] Trying to get property 'client_id' of non-object Here’s what the dataFile looks like. <?php // This doesn't work. return [ 'client_one' => [ 'client_id' => 'mytestclient', ], ]; After some experimentation, we’ve found that we need to set the _id within the dataFile in order to get $I->grabFixture to work properly. ...

<span title='2021-07-17 13:00:00.017 +0800 +0800'>July 17, 2021</span>
Yii

Yii 2 Codeception API Test

My API uses the Yii2 advanced template. It uses symfony/dotenv to read the .env. The .env is loaded in all the configurations. $dotenv = new Symfony\Component\Dotenv\Dotenv; $dotenv->load(__DIR__ . '/../../.env'); While running the Codeception api test, I came across this error. [PHPUnit\Framework\Exception] Undefined index: HOST at ../common/config/main-local.php:7 After much digging around, I’ve realized that I need to load Symfony\Component\Dotenv\Dotenv in common/config/codeception-local.php Thus, I have updated that file in the environment folder, and reran php init. ...

<span title='2021-06-30 21:00:00.016 +0800 +0800'>June 30, 2021</span>
Yii

Yii 2 404 Not Found

After setting up a new application using Yii 2 Advanced Project Template, the very first time you want to load your new controller, if you are met with a 404 Not Found, one very likely cause is that you do not have a .htaccess in your web folder.

<span title='2021-06-29 13:00:00.001 +0800 +0800'>June 29, 2021</span>
Yii

Yii2 URL manager cache class

You might come across the below warning if you have not set a cache to your Yii2 application while using URL manager. Unable to use cache for URL manager: Failed to instantiate component or class "cache". In order to remove that warning, setup a cache in your application’s configuration. We are using the DummyCache class here as we have yet to decide on one that we want to use. 'components' => [ 'cache' => [ 'class' => 'yii\caching\DummyCache', // FIXME ], ],

<span title='2020-04-09 13:00:00 +0800 +0800'>April 9, 2020</span>
Yii

Setup User and RBAC Management in Yii2

My goal for this exercise is to have User and RBAC management in my Yii 2 application. I’ve started out with the Yii 2 Basic Project Template and I would be adding the Yii2-user and Yii2-rbac extensions to my web application. Here are some of the steps I have taken to install and configure the extensions. Step 1: Installation Install the packages. composer require dektrium/yii2-user composer require dektrium/yii2-rbac Step 2: Configure the web application NOTE: Make sure that you don’t have user component configuration in your config files. Add the user and rbac module to the web applcation config. ...

<span title='2019-08-26 23:05:00 +0800 +0800'>August 26, 2019</span>
Nginx

Nginx Configuration for Multiple Yii2 Site Under Same Domain

This nginx configuration will allow you to run multiple instances of Yii2 in sub folders under the same domain. Eg. http://advanced.local/ and http://advanced.local/subfolder/ server { listen 80; # listen for IPv4 server_name advanced.local; set $root_path /var/www/main/web; root $root_path; index index.html index.php; charset utf-8; client_max_body_size 100M; location / { root $root_path; try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { try_files $uri /$uri =404; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ^~ /subfolder { alias /var/www/subfolder/web; if (!-e $request_filename) { rewrite ^ /subfolder/index.php last; } location ~ \.php$ { if (!-f $request_filename) { return 404; } include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; } } location ~* \.(htaccess|htpasswd|svn|git) { deny all; } }

<span title='2017-12-27 16:03:00 +0800 +0800'>December 27, 2017</span>