-
-
Notifications
You must be signed in to change notification settings - Fork 96
Add third-party application integration cookbook recipe #476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
samdark
wants to merge
1
commit into
master
Choose a base branch
from
cookbook-yii-in-third-party-apps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # Using Yii in third-party applications | ||
|
|
||
| Sometimes a project already has an entry point owned by another framework, CMS, or legacy application, but you still | ||
| want to reuse Yii services: configuration, DI definitions, domain services, logging, mailers, database connections, or | ||
| console-oriented application code. | ||
|
|
||
| Yii3 has no global application singleton. Bootstrap the Yii container explicitly and pass the services you need to the | ||
| third-party code. | ||
|
|
||
| ## Bootstrap the Yii container | ||
|
|
||
| Create a small adapter in the host application, for example `bootstrap-yii-services.php`: | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Environment; | ||
| use Psr\Container\ContainerInterface; | ||
| use Yiisoft\Yii\Runner\BootstrapRunner; | ||
| use Yiisoft\Yii\Runner\Console\ConsoleApplicationRunner; | ||
|
|
||
| function yiiContainer(string $yiiRoot): ContainerInterface | ||
| { | ||
| require_once $yiiRoot . '/src/bootstrap.php'; | ||
|
|
||
| $runner = new ConsoleApplicationRunner( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we avoid using runner if another app already handles the entry point? I don't think we need everything that the runner creates as well. |
||
| rootPath: $yiiRoot, | ||
| debug: Environment::appDebug(), | ||
| checkEvents: false, | ||
| environment: Environment::appEnv(), | ||
| ); | ||
|
|
||
| $container = $runner->getContainer(); | ||
| $bootstrap = $runner->getConfig()->get('bootstrap-console'); | ||
|
|
||
| (new BootstrapRunner($container, $bootstrap))->run(); | ||
|
|
||
| return $container; | ||
| } | ||
| ``` | ||
|
|
||
| The console runner is used only as a convenient way to build configuration and DI container groups. The code does not | ||
| call `$runner->run()`, so it does not start a Yii console application and does not call `exit()`. | ||
|
|
||
| Use `ConsoleApplicationRunner` when you need common services and console-safe services. Use `HttpApplicationRunner` | ||
| instead only when you intentionally need web-specific definitions from the `di-web` group, such as PSR-17 HTTP factories | ||
| or web middleware dependencies. | ||
|
|
||
| ## Get Yii services from the host application | ||
|
|
||
| Use the adapter from the third-party application: | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Shared\ApplicationParams; | ||
|
|
||
| require_once __DIR__ . '/path/to/yii-app/vendor/autoload.php'; | ||
| require_once __DIR__ . '/bootstrap-yii-services.php'; | ||
|
|
||
| $container = yiiContainer(__DIR__ . '/path/to/yii-app'); | ||
|
|
||
| /** @var ApplicationParams $params */ | ||
| $params = $container->get(ApplicationParams::class); | ||
|
|
||
| echo $params->name; | ||
| ``` | ||
|
|
||
| In real integrations, prefer getting your own application services instead of framework infrastructure. For example, | ||
| expose `InvoiceSender`, `ReportBuilder`, or `CatalogImporter` from Yii and call that service from the host application. | ||
|
|
||
| ## Keep boundaries explicit | ||
|
|
||
| Avoid mixing request lifecycles. Let the host application continue to own its HTTP request and response. Let Yii provide | ||
| services that do not assume Yii is handling the current request. | ||
|
|
||
| Good candidates for reuse: | ||
|
|
||
| - domain services; | ||
| - repositories and database connections; | ||
| - mailers and notification services; | ||
| - loggers; | ||
| - validators; | ||
| - queue producers; | ||
| - command-like application services. | ||
|
|
||
| Avoid pulling in Yii actions, middleware stacks, sessions, CSRF middleware, or view rendering unless the host application | ||
| explicitly delegates that whole responsibility to Yii. | ||
|
|
||
| ## Configuration groups | ||
|
|
||
| The default application template separates configuration by group: | ||
|
|
||
| - `di` is shared by web and console entry points; | ||
| - `di-console` is console-specific and includes `di` in the template; | ||
| - `di-web` is web-specific and includes `di`; | ||
| - `params-console` and `params-web` extend common `params`. | ||
|
|
||
| Choose the smallest group that contains the services you need. If the host application only needs domain services, keep | ||
| those definitions in `config/common/di/*.php` so both Yii and the third-party app can use them without web-only | ||
| dependencies. | ||
|
|
||
| After changing `config/configuration.php`, rebuild the merge plan: | ||
|
|
||
| ```shell | ||
| composer yii-config-rebuild | ||
| ``` | ||
|
|
||
| ## Error handling and logging | ||
|
|
||
| The host application should remain responsible for top-level error handling. Catch exceptions at the integration | ||
| boundary when the host app needs to convert them into its own response or error format. | ||
|
|
||
| Yii services can still use `Psr\Log\LoggerInterface`. Configure the Yii logger to write to the same destination as the | ||
| host application or bridge both applications to the same PSR-3 logger. | ||
|
|
||
| ## Long-running host processes | ||
|
|
||
| If the third-party application is a worker, daemon, or event-loop server, avoid mutable shared state in services. Yii | ||
| services resolved from the container are shared by default, so design them as stateless services or create a fresh | ||
| container for each isolated job when stateful dependencies are unavoidable. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid using runner if another app already handles the entry point?