10

I have application which has core website, api and admin area. 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?

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)
  3. create different Symfony2 project for admin area and api.
  4. or your wise words :)
Basit
  • 16,316
  • 31
  • 93
  • 154
  • 1
    you can try create environnement for doing this follow dev exemple for doing this `if (in_array($this->getEnvironment(), array('dev', 'test'))) {` in appKernel – pietro Feb 16 '16 at 15:02
  • What did you think about my previous comment ? – pietro Feb 25 '16 at 10:26
  • Your forgot one option: split code in several bundles, it was already discussed here: [Should everything really be a bundle in Symfony 2.x?](http://stackoverflow.com/q/9999433/2257664) or [Symfony2 conceptual issue: general bundles vs. specific ones](http://stackoverflow.com/q/8012191/2257664). – A.L Feb 28 '16 at 13:50
  • @Basit I was sure if you splitted your configuration in one or several bundles so I prefered to share the links. Anyway with pietro's idea and [new environments](http://symfony.com/doc/current/cookbook/configuration/environments.html#creating-a-new-environment) it seems that you'll be able to load different bundles depending of your environment, which should avoid loading unneeded bundles. – A.L Feb 28 '16 at 22:18
  • @Basit by this solution you can configure routing, parameters, ... for each environnement. – pietro Mar 03 '16 at 08:10

4 Answers4

2

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.

Have multiple kernel for different part of application (api, admin and core website)

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). 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.

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).

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). 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.

But it's case only for people who measure every millisecond of response time. And depends on your/3dparty bundles quality. If all there is perfectly ok then you don't need to mess with Kernels.

Paweł Mikołajczuk
  • 3,692
  • 25
  • 32
2

You can define more "environments".

For example :

In 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;
   }
}
pietro
  • 902
  • 10
  • 22
  • I don't think this is right idiomatically. The API bundle might be loaded in both PRODUCTION/DEVELOPMENT environments providing us with a different behavior. So, for example, the ENV is a mode of Core App, Api application areas. Also we can have STAGING, TESTING environments. – lexeme Sep 12 '17 at 14:37
0

It's personal choice, but I have a similar project and I have a publicBundle, adminBundle and apiBundle all within the same project.

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? :)

NB: You terminology is a little confusing, I think by Kernel you mean Bundle.

Egg
  • 1,782
  • 1
  • 12
  • 28
  • No, by kernel I mean kernel... I have all the bundles.. just not sure if have lots of bundles in production would be good choice, because not all bundles are running in main website. – Basit Feb 16 '16 at 09:48
  • Having multiple bundles (we're only talking about a couple here) really doesn't affect performance. If everything is for the same domain, same branding, same URL, then use the one kernel. – Egg Feb 16 '16 at 10:36
  • couple extra bundles that are not required are almost close to 10+ bundles, so do really fancy stuff and events handling.. like sonataAdminBundle – Basit Feb 17 '16 at 00:25
  • @Basit I suggest you to add the content of this last comment in your question, the fact that you have 10+ bundles (this number wasn't in your question) and that they *do really fancy stuff* (loading configuration? etc.) can have an impact of in performance. It's worth being mentioned in your question. – A.L Feb 28 '16 at 22:25
0

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.

Note :

If you are going to separate your app in two big bundles (i.e. Admin & Api), and that the two share the same entities, you will surely have to do a choice.

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.

Tjaari76
  • 46
  • 4
  • I do have multiple bundles, but creating different things in bundles, does not really help of what I was asking. I was asking about performance, how that will effect, even if you do have things into different bundles, but thous bundles you don't need it until its path is called.. – Basit Mar 03 '16 at 08:04