使用MicrokernEltrait:Symfony的单个文件应用程序(SFA):一种简化的方法
Symfony 2.8和3.0引入了单个文件应用程序(SFAS),这是一种构建Symfony应用程序的简化方法,对于微服务或较小的项目尤其有用。 这是通过
实现的。 本文探讨了SFA,其好处,局限性以及它们与完整的Symfony设置相比。MicroKernelTrait
>传统的符号应用程序可能涉及许多文件,但SFA的目的是更简洁。 但是,这种方法并不意味着真正的单
>要构建SFA,您需要Web服务器和作曲家。 Laravel代客或Homestead等本地开发环境改进了简化的设置。>
>步骤1:最小符号安装
> >使用作曲家安装Core Symfony软件包:
create
和<code class="language-bash">composer require symfony/symfony</code>>在您的项目root中的目录。
app
步骤2:前控制器(web
)
>
此文件接收请求,并将其路由到应用程序内核:web/app_dev.php
的方法。
><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>
步骤3:内核类(app/SfaKernel.php
)loadClassCache()
此类扩展了Symfony的并使用app/SfaKernel.php
和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()
虽然SFA不打算用于大规模应用,但它们的理想是:home()
greet()
小型,独立服务。>
概念证明项目:快速原型想法。
MicroKernelTrait
(注意:原始输入中的图像URL都是相同的。我一直保持原样,但是在现实世界中,它们可能会有所不同。
以上是单文件符号应用程序?是的,与微芯片!的详细内容。更多信息请关注PHP中文网其他相关文章!