2

I'm sure this is easy, but I'm totally stuck here.

I'm using sf2 to make a site with multiple "applications", but I want a mainpage to display some kind of index of them and I'm not sure on how to do this. Let's think of this example (actually, it's not what I'm doing but we could use this):

I have a page for some video game consoles (for now, PS3, 360, WII, PSV and NDS). They will be located in sites like ps3.domain.com, 360.domain.com, nds.domain.com and so on. Every one of them is actually different in it's logics, they are not clones (one "app" for every one of them), they are mostly independent, except for the core and the user/community stuff, which they are sharing. But in www.domain.com or just domain.com, I need to have links to all of them, and I'm not sure where to put this, or how to make a "global" controller above all others.

Could somebody help me?

mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95
Ikzer
  • 527
  • 11
  • 29

1 Answers1

7

If your applications are really different, here how I'd do:

Structure:

// src/:
    Sybio\Bundle\CoreBundle // Shared entities, config and tools
    Sybio\Bundle\PortalBundle // Your main portal, domain.com
    Sybio\Bundle\Ps3Bundle // Ps3 bundle
    Sybio\Bundle\XboxBundle // Xbox bundle
    // etc ...

Perhaps a better thing, add all console bundles inside a "Console" repository:

Sybio\Bundle\CoreBundle // Shared entities, config and tools
Sybio\Bundle\PortalBundle // Your main portal, domain.com
Sybio\Bundle\Console\Ps3Bundle // Ps3 bundle
Sybio\Bundle\Console\XboxBundle // Xbox bundle
// etc ...

CoreBundle is used to share entities between the other bundles, but not only: it will be the bundle that contains specific part of templates that are include on all bundles.

After, you create one web controller per console:

web/app.php // Your main site
web/app_ps3.php // Ps3 app
web/app_xbox.php // Xbox app
// etc...

See inside app_ps3.php, replace the first param of AppKernel constructor by the name of the console:

// ...    
$kernel = new AppKernel('ps3', false);
// ...

You can duplicate each controller in dev version if you want, like app_dev.php...

Now, the framework will load different environment depending of the used webcontroller.

For example, your domain "ps3.domain.com" will use web/app_ps3.php (we will see how to do that at the end).

The framework will load the config of the app in app/AppKernel.php:

public function registerContainerConfiguration(LoaderInterface $loader)
{
    $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}

If the visitor hit web/app_ps3.php, the framework will load config_ps3.yml instead of the classical config_dev.php or config_prod.php. You'll now play with bundle dependencies !

Create for each console a "app/config_myconsole.php" and also a "app/routing_myconsole.yml".

In config_ps3.yml (and other consoles), load your general config:

// config_ps3.yml
imports:
    - { resource: config.yml }

After this line override the routing inclusion for ps3 routing file:

// config_ps3.yml

// ...

framework:
    router:
        resource: "%kernel.root_dir%/config/routing_ps3.yml"

To resume, if a user hit web/app_ps3.yml, the file config_ps3.yml is loaded, then the general config.yml file, then routing_ps3.yml (which replace routing.yml).

You can also just import the config of the wanted bundle, for each config file, this can lighten the load if you have many bundles:

// config_ps3.yml
imports:
- { resource: config.yml }
- { resource: "@SybioCoreBundle/Resources/config/services.yml" }
- { resource: @SybioPs3Bundle/Resources/config/services.yml }

Each console application used is own routes, and also some shared routes and actions located in CoreBundle.

For each application, you need to load its console routes and the corebundle routes, here an example of routing_ps3.yml:

// routing_ps3.yml:
SybioCoreBundle:
    resource: "@SybioCoreBundle/Controller/"
    type:     annotation
    prefix:   /

SybioPs3Bundle:
    resource: "@SybioPs3Bundle/Controller/"
    type:     annotation
    prefix:   /

If the user hit web/app_ps3.yml, routes from Ps3Bundle and CoreBundle are loaded... If the user is on http://ps3.domain.com/, il will hit the route "/" from Ps3Bundle, and not the route "/" from another console or from the portal bundle, because you don't load them !

If you've got a generic page, like "/stats/", that is shared between all applications, just create this route in CoreBundle, so that you don't have to duplicate the code on each bundle. As you included CoreBundle routes with Ps3Bundle, the framework will search the route in both bundles.

This route will be accessible here: ps3.domain.com/stats/, xbox.domain.com/stats/...

To finish, the last thing you need to do is to use the right webcontroller depending on the subdomain, here how to process:

// web/.htaccess
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Hit ps3 app
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} !^ps3\.domain.com$ [NC]
    RewriteRule ^(.*)$ app_ps3.php [QSA,L]

    # Hit xbox app
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} !^xbox\.domain.com$ [NC]
    RewriteRule ^(.*)$ app_xbox.php [QSA,L]

    # Hit your main portal
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} !^www\.domain.com$ [NC]
    RewriteRule ^(.*)$ app.php [QSA,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

I'm not really comfortable with vhosts, but normally it works !

So that's it, you know how to play with different config to load different applications built on the same core !

Cheers.

Sybio
  • 8,565
  • 3
  • 44
  • 53
  • Thankyou very much! This helped me a lot and actually solved me many more problems than my original one ^^ – Ikzer Oct 28 '12 at 17:39
  • One more piece of advice - get over the "everything is a bundle" mentality. Not everything should be a bundle. Entities for example are not Symfony specific (could be used outside of Symfony app) and you can safely put them into its own namespace (not a bundle). – Jakub Zalas Oct 29 '12 at 10:21