ホームページ >バックエンド開発 >PHPチュートリアル >シングルファイルSymfonyアプリ?はい、microkerneltraitで!
Microkerneltraitを使用したSymfonyのシングルファイルアプリケーション(SFAS):合理化されたアプローチ
Symfony 2.8および3.0は、特にマイクロサービスまたは小規模プロジェクトに役立つSymfonyアプリケーションを構築するための単純化されたアプローチである、単一ファイルアプリケーション(SFA)を導入しました。 これは、
を通じて達成されます。 この記事では、SFA、その利点、制限、およびそれらが完全なSymfonyセットアップとどのように比較されるかについて説明します。 MicroKernelTrait
従来のSymfonyアプリケーションには多数のファイルが含まれる可能性がありますが、SFAはより簡潔な構造を目指しています。 ただし、このアプローチは、真のシングル
SFAを構築するには、Webサーバーと作曲家が必要です。 Laravel ValetやHomesteadのようなローカル開発環境が改善されました。 ステップ1:最小限のsymfonyインストールComposer:
を使用してコアSymfonyパッケージをインストールします
プロジェクトルート内のディレクトリを作成します。
ステップ2:フロントコントローラー()
<code class="language-bash">composer require symfony/symfony</code>
このファイルはリクエストを受信し、アプリケーションカーネルにルーティングします:app
web
にあります。 この最小限のセットアップで簡単にするためにメソッドは省略されています。
web/app_dev.php
ステップ3:カーネルクラス(
このクラスはsymfonyの
<code class="language-php"><?php use Symfony\Component\HttpFoundation\Request; require __DIR__.'/../vendor/autoload.php'; require __DIR__ . '/../app/SfaKernel.php'; $kernel = new SfaKernel('dev', true); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send(); $kernel->terminate($request, $response);</code>を拡張し、
:app/SfaKernel.php
を利用します
loadClassCache()
メソッドはアプリケーションルートを定義し、依存関係噴射コンテナを構成します。 app/SfaKernel.php
およびメソッドは、単純なコントローラーとして機能します
ユースケースと考慮事項 Microservices:
簡単なアプリケーション:
Symfony's (注:元の入力の画像URLはすべて同一でした。私はそれらをそのまま保持していましたが、実際のシナリオでは、おそらく異なるでしょう。Kernel
MicroKernelTrait
<code class="language-php"><?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
class SfaKernel extends Kernel
{
use MicroKernelTrait;
public function registerBundles()
{
return [
new FrameworkBundle(),
];
}
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/', 'kernel:home');
$routes->add('/greet/{who}', 'kernel:greet');
}
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
$c->loadFromExtension('framework', [
'secret' => 'micr0', // Replace with a unique secret
]);
}
public function home() {
return new Response('<p>Home, sweet home</p>');
}
public function greet($who)
{
return new Response("<h1>Greeting</h1>
<p>Hello $who</p>");
}
}</code>
configureRoutes()
小規模な独立サービス。configureContainer()
home()
greet()
概念の証明プロジェクト:
以上がシングルファイルSymfonyアプリ?はい、microkerneltraitで!の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。