1. 云栖社区>
  2. PHP教程>
  3. 正文

New in Symfony 2.8: Symfony as a Microframework

作者:用户 来源:互联网 时间:2017-12-01 14:02:19

New in Symfony 2.8: Symfony as a Microframework - 摘要: 本文讲的是New in Symfony 2.8: Symfony as a Microframework, Contributed by Ryan Weaver , Fabien Potencier in #15990 . A microframework is a term used to refer to "mini

New in Symfony 2.8: Symfony as a Microframework

Contributed by

Ryan Weaver , Fabien Potencier in #15990 .

A microframework is a term used to refer to "minimalistic web application frameworks". Developers usually associate this term with "fast and small frameworks", such as Silex . But you can also think of microframeworks as simple and less-opinionated fameworks where you opt-in to the the architecture-related decisions.

Thanks to its flexible internal architecture, it has been possible to use Symfony as a microframework since day one. However, few developers have used Symfony in that way because it wasn't completely convenient.

Symfony 2.8 introduces a new microkernel traitto greatly simplify the creation of single-file (or just smaller) Symfony applications. A "Hello World" application using Symfony as a microframework looks like this:

// app/MicroKernel.phpuse Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait;use Symfony/Component/Config/Loader/LoaderInterface;use Symfony/Component/DependencyInjection/ContainerBuilder;use Symfony/Component/HttpFoundation/Response;use Symfony/Component/HttpKernel/Kernel;use Symfony/Component/Routing/RouteCollectionBuilder;class MicroKernel extends Kernel{use MicroKernelTrait;public function registerBundles(){return array(new Symfony/Bundle/FrameworkBundle/FrameworkBundle());}protected function configureRoutes(RouteCollectionBuilder $routes){$routes->add('/', 'kernel:indexAction', 'index');}protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader){$c->loadFromExtension('framework', ['secret' => '12345']);}public function indexAction(){return new Response('Hello World');}}

A single MicroKernel class enables the bundles, configures them, defines the routing and even holds the controller code. If you exclude the mandatory use imports and the function declarations, the above example has exactly 4 lines of PHP code . Think about that for a moment: a fully-functional Symfony application in just 4 lines of code!

As you can imagine, using Symfony as a microframework improves its performance significantly, because less features and bundles are enabled. In this simple example, the difference is up to 3x in CPU performance and 2x in memory consumption comparing it with a default Symfony Standard Edition application:

New in Symfony 2.8: Symfony as a Microframework

The best thing about the Symfony microframework is that you are building your application on the shoulders of Symfony, meaning that you won't face any of the usual restrictions of the microframeworks. All the incredible Symfony features and bundles are ready to use in case you need them as your application grows.

In the following example, the Hello World application is expanded to add Twig templating support, the Web Debug toolbar and the Symfony Profiler. The result is still a single-file application:

// app/MicroKernel.phpclass MicroKernel extends Kernel{use MicroKernelTrait;public function registerBundles(){$bundles = array(new Symfony/Bundle/FrameworkBundle/FrameworkBundle(),new Symfony/Bundle/TwigBundle/TwigBundle(),);if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {$bundles[] = new Symfony/Bundle/WebProfilerBundle/WebProfilerBundle();}return $bundles;}protected function configureRoutes(RouteCollectionBuilder $routes){$routes->mount('/_wdt', $routes->import('@WebProfilerBundle/Resources/config/routing/wdt.xml'));$routes->mount('/_profiler', $routes->import('@WebProfilerBundle/Resources/config/routing/profiler.xml'));$routes->add('/', 'kernel:indexAction', 'index');}protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader){// load bundles' configuration$c->loadFromExtension('framework', ['secret' => '12345','profiler' => null,'templating' => ['engines' => ['twig']],]);$c->loadFromExtension('web_profiler', ['toolbar' => true]);// add configuration parameters$c->setParameter('mail_sender', '[email protected]');// register services$c->register('app.markdown', 'AppBundle//Service//Parser//Markdown');}public function indexAction(){return $this->container->get('templating')->renderResponse('index.html.twig');}}

Creating single-file applications is not the main purpose of this microkernel. The real use-case is to create smaller Symfony applications , perhaps having just one services.yml file, one config.yml file per environment and the routing defined as annotations in regular controller classes. This is the full code needed to implement this use case:

// app/MicroKernel.php// ...class MicroKernel extends Kernel{use MicroKernelTrait;public function registerBundles(){return array(new Sensio/Bundle/FrameworkExtraBundle/SensioFrameworkExtraBundle(),new Symfony/Bundle/FrameworkBundle/FrameworkBundle(),new Symfony/Bundle/TwigBundle/TwigBundle(),new AppBundle/AppBundle(),);}protected function configureRoutes(RouteCollectionBuilder $routes){$routes->mount('/', $routes->import('@AppBundle/Controller', 'annotation'));}protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader){$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');$loader->load(__DIR__.'/config/services.yml');}}

And don't forget to update the front controller to use this new microkernel instead of the usual AppKernel :

// web/app.phpuse Symfony/Component/HttpFoundation/Request;$loader = require __DIR__.'/../app/autoload.php';require_once __DIR__.'/../app/MicroKernel.php';$app = new MicroKernel('prod', false);$app->loadClassCache();$app->handle(Request::createFromGlobals())->send();

So if you're choosing between a microframework or the full-stack framework, you have a new possibility. Now you can have both at the same time in the same application and without compromising any features.

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。