Symfony2 子应用程序的主页 [英] Main page to sub applications on Symfony2

查看:31
本文介绍了Symfony2 子应用程序的主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这很容易,但我完全被困在这里.

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

我正在使用 sf2 制作一个包含多个应用程序"的网站,但我想要一个主页来显示它们的某种索引,但我不确定如何执行此操作.让我们想想这个例子(实际上,这不是我在做的,但我们可以使用它):

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

我有一些视频游戏机的页面(目前,PS3、360、WII、PSV 和 NDS).它们将位于 ps3.domain.com360.domain.comnds.domain.com 等网站中.它们中的每一个实际上在逻辑上都不同,它们不是克隆(每个应用程序"),它们大多是独立的,除了核心和用户/社区的东西,它们是共享的.但在 www.domain.com 或只是 domain.com,我需要有指向所有这些的链接,但我不知道把它放在哪里,或者如何使全局"控制器高于所有其他控制器.

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.

有人可以帮我吗?

推荐答案

如果您的应用程序真的与众不同,我会怎么做:

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

结构:

// 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 用于在其他包之间共享实体,但不仅限于:它是包含所有包中包含的模板特定部分的包.

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.

之后,您为每个控制台创建一个 Web 控制器:

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

查看app_ps3.php里面,将AppKernel构造函数的第一个参数替换为控制台的名字:

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

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

如果需要,您可以复制开发版本中的每个控制器,例如 app_dev.php...

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

现在,框架将根据使用的 webcontroller 加载不同的环境.

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

例如,您的域ps3.domain.com"将使用 web/app_ps3.php(我们将在最后看到如何做到这一点).

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

框架将在 app/AppKernel.php 中加载应用的配置:

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');
}

如果访问者点击 web/app_ps3.php,框架将加载 config_ps3.yml 而不是经典的 config_dev.php 或 config_prod.php.您现在将使用捆绑依赖项!

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 !

为每个控制台创建一个app/config_myconsole.php"和一个app/routing_myconsole.yml".

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

在 config_ps3.yml(和其他控制台)中,加载您的常规配置:

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

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

在此行之后覆盖 ps3 路由文件的路由包含:

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

// config_ps3.yml

// ...

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

要恢复,如果用户点击 web/app_ps3.yml,则加载文件 config_ps3.yml,然后是通用 config.yml 文件,然后是routing_ps3.yml(替换routing.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 }

使用的每个控制台应用程序都有自己的路由,还有一些位于 CoreBundle 中的共享路由和操作.

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

对于每个应用程序,你需要加载它的控制台路由和核心包路由,这里是一个routing_ps3.yml的例子:

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:   /

如果用户点击 web/app_ps3.yml,来自 Ps3Bundle 和 CoreBundle 的路由被加载...如果用户在 http://ps3.domain.com/ 上,我会点击路由/"来自 Ps3Bundle,而不是来自另一个控制台或门户包的路由/",因为您没有加载它们!

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 !

如果您有一个通用页面,例如/stats/",在所有应用程序之间共享,只需在 CoreBundle 中创建此路由,这样您就不必在每个包上复制代码.当您在 Ps3Bundle 中包含 CoreBundle 路由时,框架将在两个包中搜索路由.

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.

此路由可在此处访问:ps3.domain.com/stats/、xbox.domain.com/stats/...

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

完成,你需要做的最后一件事是根据子域使用正确的webcontroller,这里如何处理:

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 !

干杯.

这篇关于Symfony2 子应用程序的主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆