Yii2 dev digest #5 Jan'14 > stdout.in Ievgen Kuzminov IT blog

Yii2 dev digest #5 Jan'14

Jan 15, 2014, 7:38:15 PM

New Year holidays are coming to the end and finally I had some time to review all those issues and news on GitHub.
And you know - it seems that Yii core dev team was working all this time :)

[RU]Судя по географии посещений этих дайджестов - мне надо скорее доделывать поддержку мультиязычных постов и локализовать посты на русский язык ;)[/RU]

Yii2 Tips & Tricks

Just avoiding new and replacing it with a string + method call that does exactly the same is pointless. The thing is that you can do the following. Let's assume we have hardcoded class name:

class Rocket
{
  public function fly()
  {
    $engine = new Engine();
    return $engine->work();
  }
}
$rocket = new Rocket();
$rocket->fly();

You can do it better:

class Rocket
{
  public $engine = '\app\rocket\lightspeed\Engine';

  public function fly()
  {
    $engine = Yii::createObject($this->engine);
    return $engine->work();
  }
}
$rocket = new Rocket();
$rocket->engine = [
  'class' => 'app\rocket\gasoline\Engine',
  'fuel' => 'Gas',
];
if (!$rocket->fly()) {
  echo 'Well, that was pretty old engine...';
}

Extension MAY use yii2- in the composer package name (e.g vendor\yii2-api-adapter or vendor\my-yii2-package (URL).

You can enable/disable CSRF validation per controller/action by setting Controller::enableCsrfValidation in beforeAction or through some behavior.

relation names are case sensitive now in Yii 2, unlike 1.1.

You can use i18n in your views to provide support for different languages. For example, if you have view views/site/index.php and you want to create special case for russian language, you create ru-RU folder under the view path of current controller/widget and put there file for russian language as follows views/site/ru-RU/index.php.

\Yii::$app->urlManager->createUrl('date-time/fast-forward',['id' => 105]); returns /index.php/date-time/fast-forward?id=105 instead of /index.php/date-time/fast-forward/id/105 @qiangxue : If I remember correctly, Yii2 is no longer accepting name/value/name/value arguments automatically but it should be verified. If it's true, this PR should be merged as is. Yes, this is by design for Yii 2. There's no benefit for this format.

Because it follows the same pattern as other static helper classes: Xyz and BaseXyz.

echo $this->createurl('/date-time/fast-forward', ['id' => 105]); // url for action the case sensitive controller, DateTimeController::actionFastForward

Anyway if framework does not provide some feature you can always bypass it. I use the following approach to reuse migrations for the unit/functional tests:

abstract class TestCaseDb extends TestCase
{
    /**
     * @var boolean indicates if database migrations have been applied.
     */
    protected static $_isDbMigrationApplied = false;

    /**
     * Apply db migrations to the current database.
     * This method allows to keep test database up to date.
     */
    public static function applyDbMigrations()
    {
        $migrateController = new MigrateController('migrate', Yii::$app);
        $migrateController->interactive = false;
        ob_start();
        ob_implicit_flush();
        $migrateController->runAction('up');
        ob_clean();
    }

    public static function setUpBeforeClass()
    {
        if (!static::$_isDbMigrationApplied) {
            static::applyDbMigrations();
            static::$_isDbMigrationApplied = true;
        }
    }
}

For caching the result of database queries you can wrap them in calls to yii\db\Connection::beginCache() and yii\db\Connection::endCache() :

$connection->beginCache(60); // cache all query results for 60 seconds.
// your db query code here...
$connection->endCache();

read the docs :)

Read previous digests

comments powered by Disqus
Ievgen
Kuzminov "iJackUA"
Web Team Lead
at MobiDev (Kharkiv, Ukraine)
Code in Ruby and Elixir, but still love PHP. Explore ES6 and Vue.js. Explore databases, use Ubuntu and MacOS, think about IT people and management

Notes