yiisoft/yii2-app-advanced
Yii 2 Advanced Application Template: a starter project for building Yii2 apps with separate frontend and backend, shared common code, environment-specific configs, and ready-to-use tools for development, testing, and deployment.
Installation Clone the template and set up dependencies:
git clone https://github.com/yiisoft/yii2-app-advanced.git my-project
cd my-project
composer install --prefer-dist
Environment Setup
.env.example to .env and configure database (DB_*), mail (MAIL_*), and security keys (APP_KEY, SECURITY_KEY).common/config/main-local.php with local overrides (e.g., debug mode, logging).First Use Case: Console Commands Run a built-in command (e.g., migrations):
php yii migrate/up
Key files to inspect:
console/config/main.php (console app config)common/console/controllers/ (custom commands)Modular Structure
frontend/ and backend/ for distinct user roles. Share logic via common/ (models, components).common/models/User and reuse in both apps.API Integration
api/ for headless services. Share models/components with common/.api/controllers/v1/BaseController for auth middleware.Asset Management
yii\web\AssetBundle in frontend/web/assets/ for frontend assets.// frontend/views/site/index.php
yii\web\JqueryAsset::register($this);
Database Migrations
common/migrations/ for cross-app tables.php yii migrate/create --migrationPath=common/migrations --name=20230101_000000_create_users
yii\filters\AccessControl in backend/config/main.php for role-based access.common/config/main.php to log to runtime/logs/ (shared across apps).common/tests/ for unit tests; frontend/tests/ for integration tests.Environment Confusion
YII_ENV=dev in .env causes production-like behavior locally.common/config/main.php:
'components' => [
'request' => [
'cookieValidationKey' => YII_ENV_PROD ? $config['params']['cookieValidationKey'] : 'dev-key',
],
],
Asset Paths
@web aliases.frontend/config/main.php has:
'components' => [
'assetManager' => [
'basePath' => '@frontend/web/assets',
'baseUrl' => '@web/assets',
],
],
Database Connections
common/config/main.php DB config overrides frontend/ or backend/ configs.db component aliases:
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=frontend_db',
],
'dbBackend' => ['class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=backend_db'],
],
'enableErrorHandler' => true in common/config/main-local.php.Yii::debug() and Yii::error() in common/components/Logger.php for granular logging.php.ini for remote debugging with IDEs (e.g., PhpStorm).Custom Modules
common/modules/MyModule/ with:
// common/modules/MyModule/MyModule.php
namespace app\modules\MyModule;
class MyModule extends \yii\base\Module { ... }
common/config/main.php:
'modules' => ['myModule' => 'app\modules\MyModule\MyModule'],
Event-Driven Architecture
yii\base\Application::EVENT_AFTER_REQUEST in common/components/RequestHandler.php:
Yii::$app->on(Application::EVENT_AFTER_REQUEST, function() {
// Post-request logic (e.g., analytics)
});
Custom Console Commands
common/console/controllers/GenerateSitemapController.php:
namespace app\console\controllers;
use yii\console\Controller;
class GenerateSitemapController extends Controller {
public function actionIndex() { ... }
}
php yii generate-sitemap
How can I help you explore Laravel packages today?