Yii2 dev digest #5 Jan'14
Jan 15, 2014, 7:38:15 PMAnd you know - it seems that Yii core dev team was working all this time :)
[RU]Судя по географии посещений этих дайджестов - мне надо скорее доделывать поддержку мультиязычных постов и локализовать посты на русский язык ;)[/RU]
- People are contributing quite a lot of messages translation. Have a look and make pull request with your native language! Hey, Klingonians, where are you ! :)
- CONTRIBUTING.md appeared in Yii2 repo. With main links to contribution instructions.
- Draft implementation of REST component/controller, discussion around it. Personally I am very intersted in (REST)API support as I am dealing with (creating) a lot of them in my work. And I am looking forward whay will be proposed by @qiangxue.
- Once again started discussion about separating “attributes” and “DB attributes” for ActiveRecord or having some "data-mapper"-like approach in AR. This topic is very interesting and non-trivial, suggest you to get into it.
- Added support for installing packages conforming to PSR-4 standard, also all packages migrated to PSR-4 folder structure
- Debug module remade to use
GridView
+ArrayDataProvider
also a lot of visual enhancements were made. - Added support for retrieving sub-array element or child object property through
ArrayHelper::getValue()
. Very handy and removes boilerplateisset
checks. - API docs generator ready. Here are docs for a framework and for extensions
- Added Image Helper based on Imageine - that's really long awaited thing !
- jQuery 2.0 now distributed with Yii2 by default, but it is still being discussed
- Added postgresql
QueryBuilder::checkIntegrity
andQueryBuilder::resetSequence
usefull for Postgres fuxtures. - NO plans to adopt PSR-3 compliant logger class
- Charset is now explicitly set to UTF-8 when serving JSON
- Discussion on test additions - AspectMock not included, Specify included. Personally I like
Specify
sugar very much. - DOCS - i18n in views
- Faker fixture generator integrated and DOC for Fixtures
- Codeception tests added to bacis app
- AuthClient Extension usage documentation
- DICSUSSION - Display current routes
- More thougnts on Admin Panel for advanced app
- Added support for building "EXISTS" and "NOT EXISTS" query conditions
- MongoDB Session class and MongoDB Cache class
- HHVM support of Yii2 is being evaluated, not perfect yet.
- Started docs for integration Yii2 with 3rd party libraries : Using Yii in 3rd-Party Systems, Using Yii2 with Yii1.
- DISCUSSION : Fixtures as classes with custom initialisers. It could open wide possibilities like fixture dependecy, complex DB structures creation before adding fixture data in db etc.
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.gvendor\yii2-api-adapter
orvendor\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 createru-RU
folder under the view path of current controller/widget and put there file for russian language as followsviews/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()
andyii\db\Connection::endCache()
:
$connection->beginCache(60); // cache all query results for 60 seconds.
// your db query code here...
$connection->endCache();
read the docs :)