yiisoft/yii2-dev
Yii 2 is a modern, fast, secure PHP framework with sensible defaults and flexible configuration. A solid foundation for building web applications, with comprehensive guides and API docs. Requires PHP 7.4+ (best on PHP 8).
Installation:
composer require yiisoft/yii2
Follow the Definitive Guide for basic setup (e.g., basic or advanced templates).
First Use Case:
use yii\web\View;
$view = new View();
echo $view->render('path/to/view.php', ['data' => 'value']);
use yii\caching\Cache;
Yii::$app->cache->set('key', 'value', 3600); // Cache for 1 hour
Key Files to Review:
config/web.php (core configuration, including cache and view settings).views/layouts/main.php (default layout for rendering).controllers/SiteController.php (basic controller structure).Template Rendering:
View::render() for dynamic content (e.g., error pages, modular UI).View::begin/end() for blocks (e.g., layouts, partials):
echo $this->beginContent('@app/views/site/index.php');
// Dynamic content here
echo $this->endContent();
Caching Strategies:
echo Yii::$app->cache->getOrSet('fragment_key', function() {
return $this->renderPartial('partial-view', ['data' => $model]);
}, 3600);
ArrayDataProvider for grids):
$dataProvider = new \yii\data\ArrayDataProvider([
'allModels' => $models,
'key' => 'id', // Supports paths (e.g., 'user.profile.id')
]);
GridView Customization:
filterSelector with closures for dynamic filters:
GridView::widget([
'dataProvider' => $dataProvider,
'filterSelector' => function($model, $attribute) {
return $attribute === 'status' ? \yii\helpers\Html::dropDownList(
$attribute,
$model->$attribute,
['active' => 'Active', 'inactive' => 'Inactive']
) : null;
},
]);
Error Handling:
ErrorHandler:
Yii::$app->errorHandler->renderFile = function($exception) {
return Yii::$app->view->render('error/custom', ['exception' => $exception]);
};
private string $name).ArrayDataProvider path support for nested data:
$dataProvider->key = 'user.profile.id'; // Access nested properties
View::renderPhpFile() to avoid collisions (fixed in 2.0.55).Yii::$app->security->validateData() for user inputs in forms/filters.Yii::$app->fixture->on('missingFixture', function($className) {
throw new \RuntimeException("Fixture $className not found.");
});
View Rendering Collisions:
View::renderPhpFile() paths may override internal variables (CVE-2026-39850).$view->renderFile(Yii::getAlias('@app/views/' . $safePath . '.php'));
Cache Key Conflicts:
ArrayDataProvider paths may fail if keys are ambiguous (e.g., user.id vs. user.id()).$dataProvider->key = ['user', 'id']; // Array syntax for clarity
PHP Version Mismatches:
phpstan or psalm to detect incompatible code:
composer require --dev phpstan/phpstan
vendor/bin/phpstan analyse
GridView Filtering Quirks:
filterSelector closures may not persist across requests.DataColumn::filter:
['attribute' => 'status',
'filter' => \yii\helpers\ArrayHelper::map(['active', 'inactive'], 'value', 'label'),
'filterInputOptions' => ['class' => 'form-control'],
],
Enable Debug Toolbar:
'components' => [
'debug' => [
'class' => 'yii\debug\Module',
'enabled' => true,
],
],
Access at /debug for cache/memory stats.
Log Cache Misses:
Yii::$app->cache->on('miss', function($event) {
Yii::error("Cache miss for key: {$event->key}", __METHOD__);
});
Validate Fixtures:
./yii fixture/load --interactive=0 --migrate
Custom Cache Tags:
Cache to support tags for invalidation:
class TaggedCache extends \yii\caching\Cache {
public function setWithTags($key, $value, $tags = [], $dependency = null) {
// Implement tag-based invalidation logic
}
}
Dynamic GridView Columns:
DataColumn::content with closures for computed fields:
['attribute' => 'full_name',
'content' => function($model) {
return $model->first_name . ' ' . $model->last_name;
}],
View Preloaders:
Yii::$app->view->preload(['module1/views/*', 'module2/views/*']);
config/web.php:
'components' => [
'cache' => [
'class' => 'yii\caching\RedisCache',
'keyPrefix' => 'app_',
],
],
views/error/ (e.g., exception.php).tests/codeception/_data/ is in Yii::getAlias('@tests') for autoloading.How can I help you explore Laravel packages today?