### Run Yii3 Web Application with Docker Source: https://github.com/yiisoft/app/blob/master/README.md Instructions for installing and running the Yii3 web application using Docker. This involves forking the repository, cloning it, updating dependencies with `make composer update`, and starting the application with `make up`. The application is then accessible via `https://localhost`. ```shell cd myproject make composer update make up ``` -------------------------------- ### Install Yii3 Web Application with Composer Source: https://github.com/yiisoft/app/blob/master/README.md This snippet shows how to create a new Yii3 web application project using Composer. It involves running the `create-project` command and navigating into the project directory. The application can then be served using the provided command. ```shell composer create-project yiisoft/app myproject cd myproject APP_ENV=dev ./yii serve ``` -------------------------------- ### Running Yii3 Application in Different Environments (Bash) Source: https://context7.com/yiisoft/app/llms.txt Provides bash commands to run the Yii3 application in various environments. Includes instructions for local development using the built-in server, production deployment, Docker setup, running tests with Codeception, and executing console commands. Environment variables like APP_ENV and APP_DEBUG are used to control application behavior. ```bash # Local development with built-in server export APP_ENV=dev export APP_DEBUG=true ./yii serve # Access at http://localhost:8080 # Production mode export APP_ENV=prod export APP_DEBUG=false php public/index.php # Docker environment make composer update make up # Access at https://localhost # Run tests export APP_ENV=test ./vendor/bin/codecept build ./yii serve > ./runtime/yii.log 2>&1 & ./vendor/bin/codecept run # Kill background server when done kill $(cat runtime/yii.pid) # Run console commands ./yii list ./yii hello ./yii user:create john john@example.com --admin # Static analysis ./vendor/bin/psalm # Code style fixing ./vendor/bin/php-cs-fixer fix ``` -------------------------------- ### Define Yii3 Application Routes Source: https://context7.com/yiisoft/app/llms.txt This snippet demonstrates how to define application routes using Yii3's router. It includes examples of simple GET routes, routes with parameters, POST routes, routes supporting multiple HTTP methods, and grouped routes with a prefix. ```php routes( // Simple GET route Route::get('/') ->action(Web\HomePage\Action::class) ->name('home'), // Route with parameters Route::get('/user/{id:\d+}') ->action(Web\User\ProfileAction::class) ->name('user/profile'), // POST route Route::post('/user/create') ->action(Web\User\CreateAction::class) ->name('user/create'), // Multiple HTTP methods Route::methods(['GET', 'POST'], '/contact') ->action(Web\Contact\FormAction::class) ->name('contact'), // Grouped routes with prefix Group::create('/api') ->routes( Route::get('/users') ->action(Web\Api\UsersAction::class) ->name('api/users'), Route::get('/posts') ->action(Web\Api\PostsAction::class) ->name('api/posts'), ), ), ]; ``` -------------------------------- ### Handle Web Requests with Yii3 View Rendering Source: https://context7.com/yiisoft/app/llms.txt This snippet demonstrates creating web action handlers in Yii3 that respond to HTTP requests by rendering views. The `Action` class utilizes `Yiisoft\Yii\View\Renderer\ViewRenderer` to render templates. The first example shows rendering a template from the same directory. The second example, `ProfileAction`, shows how to access request attributes (like 'id') and pass data to the view. ```php viewRenderer->render(__DIR__ . '/template'); } } // With parameters example: namespace App\Web\User; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Yiisoft\Yii\View\Renderer\ViewRenderer; final readonly class ProfileAction { public function __construct( private ViewRenderer $viewRenderer, ) {} public function __invoke(ServerRequestInterface $request): ResponseInterface { $userId = $request->getAttribute('id'); // Pass data to view return $this->viewRenderer->render(__DIR__ . '/template', [ 'userId' => $userId, 'username' => 'John Doe', ]); } } ``` -------------------------------- ### Create Yii3 Application Parameters Value Object Source: https://context7.com/yiisoft/app/llms.txt This snippet defines a read-only value object for storing and accessing application-wide configuration parameters in a Yii3 application. It includes examples of how to use this object in dependency injection configuration and within views/actions. ```php [ '__construct()' => [ 'name' => $params['application']['name'], 'charset' => $params['application']['charset'], 'locale' => $params['application']['locale'], ], ], ]; // Usage in views and actions (auto-injected): namespace App\Web\Settings; use App\Shared\ApplicationParams; use Psr\Http\Message\ResponseInterface; use Yiisoft\Yii\View\Renderer\ViewRenderer; final readonly class SettingsAction { public function __construct( private ViewRenderer $viewRenderer, private ApplicationParams $params, ) {} public function __invoke(): ResponseInterface { // Access application parameters $appName = $this->params->name; $charset = $this->params->charset; return $this->viewRenderer->render(__DIR__ . '/template', [ 'appName' => $appName, 'charset' => $charset, ]); } } ``` -------------------------------- ### Build and Run Tests with Codeception Source: https://github.com/yiisoft/app/blob/master/README.md These commands demonstrate how to build the test suite and then run the tests for the Yii3 application using Codeception. It includes setting the environment variable and serving the application to a log file for testing purposes. ```shell ./vendor/bin/codecept build APP_ENV=test ./yii serve > ./runtime/yii.log 2>&1 & ./vendor/bin/codecept run ``` -------------------------------- ### Build and Run Tests with Codeception (Docker) Source: https://github.com/yiisoft/app/blob/master/README.md Instructions for building and running Codeception tests when using Docker. This streamlines the testing process within a containerized environment. ```shell make codecept build make codecept run ``` -------------------------------- ### List Available Make Commands Source: https://github.com/yiisoft/app/blob/master/README.md This command lists all available `make` commands defined in the `Makefile`. This is useful for discovering various build, run, and utility tasks for the project. ```shell make help ``` -------------------------------- ### Run Static Analysis with Psalm (Docker) Source: https://github.com/yiisoft/app/blob/master/README.md Command to run Psalm static analysis using Docker. This ensures that the analysis is performed in a consistent, isolated environment. ```shell make psalm ``` -------------------------------- ### Run Static Analysis with Psalm Source: https://github.com/yiisoft/app/blob/master/README.md This snippet shows how to execute Psalm for static analysis of the Yii3 application code. It helps in identifying potential bugs and code quality issues before runtime. ```shell ./vendor/bin/psalm ``` -------------------------------- ### Console Application Bootstrap Source: https://context7.com/yiisoft/app/llms.txt Boots the console application, providing an entry point for command-line operations with environment configuration. It utilizes the `yiisoft/yii-runner-console` package. ```php run(); // Shell usage examples: // List all commands: ./yii list // Run specific command: ./yii hello // Get help: ./yii help hello // Start web server: APP_ENV=dev ./yii serve ``` -------------------------------- ### Configure Application Environment Settings with Yii3 Environment Source: https://context7.com/yiisoft/app/llms.txt This snippet demonstrates how to prepare and access application environment settings using the `App\Environment` class. It covers checking the current environment (dev, prod), debug mode, C3 mode, and retrieving host paths. Ensure `Environment::prepare()` is called before accessing any environment-related methods. Environment variables can be set via shell exports. ```php setLevels([ LogLevel::EMERGENCY, LogLevel::ERROR, LogLevel::WARNING, ]), ], ), new HtmlRenderer(), ), ); $runner->run(); // For custom middleware configuration: // Create config/web/di/middleware.php use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher; use App\Web\Middleware\AuthMiddleware; use App\Web\Middleware\LoggingMiddleware; return [ MiddlewareDispatcher::class => [ '__construct()' => [ 'middlewares' => [ LoggingMiddleware::class, AuthMiddleware::class, ], ], ], ]; ``` -------------------------------- ### Create Custom Console Commands with Yii3 and Symfony Console Source: https://context7.com/yiisoft/app/llms.txt This code illustrates how to create custom console commands in Yii3 using Symfony Console components. It defines a command named 'user:create' with required arguments ('username', 'email') and an optional flag ('--admin'). The `execute` method handles the command logic, interacting with input and output interfaces. The command can be executed via the Yii console runner. ```php addArgument('username', InputArgument::REQUIRED, 'Username') ->addArgument('email', InputArgument::REQUIRED, 'Email address') ->addOption('admin', 'a', InputOption::VALUE_NONE, 'Grant admin privileges'); } protected function execute(InputInterface $input, OutputInterface $output): int { $username = $input->getArgument('username'); $email = $input->getArgument('email'); $isAdmin = $input->getOption('admin'); // Create user logic here $output->writeln(sprintf('Creating user: %s (%s)', $username, $email)); if ($isAdmin) { $output->writeln('Granting admin privileges'); } return ExitCode::OK; } } // Run command: ./yii user:create john john@example.com --admin ``` -------------------------------- ### View Template Rendering with Yiisoft WebView Source: https://context7.com/yiisoft/app/llms.txt Renders PHP view templates, supporting layouts, view injection, and HTML encoding using `yiisoft/view`. It allows for dynamic title setting, CSS/JS registration, and safe output of dynamic content. ```php setTitle('Page Title - ' . $applicationParams->name); // Register inline CSS $this->addCssString( '.custom-class { color: #333; font-size: 16px; } '); // Register inline JavaScript $this->addJsString( 'console.log("Page loaded");', position: WebView::POSITION_END ); ?>
= Html::encode($customData) ?>