简体   繁体   English

Symfony2多核?

[英]Symfony2 multiple kernel?

I have application which has core website, api and admin area. 我有应用程序,有核心网站,API和管理区域。 I wanted to know is it bad idea to have everything in one app or should I create different Symfony2 project or should I split them into different kernels? 我想知道在一个应用程序中包含所有内容或者我应该创建不同的Symfony2项目还是应该将它们拆分为不同的内核?

I'm not sure if adding lots of bundles on same kernel will effect performance a lot or is just a little bit, which does not matter? 我不确定在相同的内核上添加大量的捆绑包是否会影响性能或者只是一点点,这无关紧要?

Following are options: 以下是选项:

  1. keep everything on same kernel, it wont make much difference 将所有内容保存在同一个内核上,它不会产生太大的影响
  2. have multiple kernel for different part of application (api, admin and core website) 有多个内核用于不同的应用程序部分(api,admin和核心网站)
  3. create different Symfony2 project for admin area and api. 为管理区域和api创建不同的Symfony2项目。
  4. or your wise words :) 或者你明智的话:)

It mostly depends on bundles quality. 它主要取决于捆绑质量。 And this how much connected they are. 这与他们有多少联系。

I would reject point 3 at start ( create different Symfony2 project for admin area and api. ) - as probably you don't build two separate applications. 我会在开始时拒绝第3点( create different Symfony2 project for admin area and api. ) - 可能你没有构建两个单独的应用程序。

Have multiple kernel for different part of application (api, admin and core website) 有多个内核用于不同的应用程序部分(api,admin和核心网站)

Common problem is created by Listeners and services in container. 常见问题是由容器中的监听器和服务创建的。 Especially when your listener should work only in one of app contexts (api/frontend/backend). 特别是当你的监听器只能在app上下文之一(api / frontend / backend)工作时。 Even if you remember to check it at very beginning of listener method (and do magic only in wanted context) then still listener can depend on injected services which need to be constructed and injected anyway. 即使你记得在监听器方法的最开始检查它(并且仅在想要的上下文中做魔术),那么仍然监听器可以依赖于需要构造和注入的注入服务。 Good example here is FOS/RestBundle: even if you configure zones then still on frontend (when view_listener is activated for api) view_handler is initialized and injected to listener - https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/config/view_response_listener.xml#L11 I'm not sure for 100% here but also disabling translations and twig (etc.) for API (most of api's don't need it) will speed it up. 这里的好例子是FOS / RestBundle:即使你配置zones然后仍然在前端(当为api激活view_listener时), view_handler被初始化并注入监听器 - https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources /config/view_response_listener.xml#L11我不确定100%在这里,但也禁用API的翻译和树枝(等)(大多数api不需要它)将加快它。

Creating separate Kernel for API context would solve that issue (in our project we use one Kernel and we had to disable that listener - as blackfire.io profiles were telling us that it saves ~15ms on every fronted request). 为API上下文创建单独的内核将解决该问题(在我们的项目中,我们使用一个内核,我们必须禁用该侦听器 - 因为blackfire.io配置文件告诉我们它在每个前端请求上节省了大约15ms)。

Creating new Kernel for API would make sure that none of API-only services/listeners will not interfere with frontend/backend rendering (it work both ways). 为API创建新的内核将确保没有API的服务/侦听器不会干扰前端/后端渲染(它可以双向工作)。 But it will create for you additional work of creating shared components used in many bundles inside project (those from different kernels) - but in world with composer it's not a huge task anymore. 但它会为你创建额外的工作来创建在项目内部的许多包中使用的共享components (来自不同内核的那些) - 但在具有编写器的世界中,它不再是一项艰巨的任务。

But it's case only for people who measure every millisecond of response time. 但只有那些测量每毫秒响应时间的人才会这样。 And depends on your/3dparty bundles quality. 并取决于您的/ 3dparty捆绑质量。 If all there is perfectly ok then you don't need to mess with Kernels. 如果一切都完全没问题那么你就不需要搞乱内核了。

You can define more "environments". 您可以定义更多“环境”。

For example : 例如 :

In AppKernel.php AppKernel.php中

public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            //new AppBundle\AppBundle()
        );

        if (in_array($this->getEnvironment(), array('api'), true)) {
            $bundles[] = new ApiBundle\ApiBundle();
            //-- Other bundle
        }
        //-- Other environments


        return $bundles;
   }
}

It's personal choice, but I have a similar project and I have a publicBundle, adminBundle and apiBundle all within the same project. 这是个人选择,但我有一个类似的项目,我在同一个项目中有一个publicBundle,adminBundle和apiBundle。

The extra performance hit is negliable but organisation is key ... that is why we're using an MVC package (Symfony) in the first place, is it not? 额外的性能打击是可以忽略的,但组织是关键...这就是为什么我们首先使用MVC包(Symfony),不是吗? :) :)

NB: You terminology is a little confusing, I think by Kernel you mean Bundle . 注意:你的术语有点令人困惑,我认为通过Kernel你的意思是Bundle

Have several kernels could not necessarily help. 有几个内核不一定有帮助。

Split your application in bundles and keep all advantages of sharing your entities (and so on) through the different parts of your application. 将您的应用程序拆分为捆绑包,并通过应用程序的不同部分保留共享实体(等等)的所有优势。

You can define separated routing/controllers/configuration that are loaded depending on the host/url. 您可以根据主机/ URL定义加载的分离路由/控制器/配置。

Note : 注意 :

If you are going to separate your app in two big bundles (ie Admin & Api), and that the two share the same entities, you will surely have to do a choice. 如果您要将您的应用程序分成两个大包(即Admin和Api),并且两者共享相同的实体,您肯定必须做出选择。

This choice may involves that one of your bundles contains too much (and non related) logic and will need to be refactored in several bundles later. 这个选择可能涉及你的一个包包含太多(和非相关)逻辑,并且需要稍后在几个包中重构。

Create a bundle per section of your application that corresponds to a set of related resources and make difference between the two parts through different contexts from configuration. 为应用程序的每个部分创建一个与一组相关资源相对应的捆绑包,并通过配置的不同上下文在两个部分之间进行区分。

Also, name your classes/namespaces sensibly. 另外,明智地命名您的类/命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM