netgusto/dynamic-subdomain-bundle

Symfony2 动态子域名处理器 Bundle

dev-master 2018-04-04 12:03 UTC

This package is not auto-updated.

Last update: 2024-09-19 14:44:42 UTC


README

安装

composer.json

"require": {
    "netgusto/dynamic-subdomain-bundle": "dev-master"
}

app/AppKernel.php

$bundles = array(
    # [...]
    new Netgusto\DynamicSubdomainBundle\NetgustoDynamicSubdomainBundle(),
    # [...]
);

配置

在 app/config.yaml

netgusto_dynamic_subdomain:
    base_host: netgusto.com
    parameter_name: ~
    entity: Acme\DemoBundle\Entity\MySite
    property: ~
  • base_host:

    • 域名基础主机,所有子域名都附加在此处
    • 必需
    • 示例: netgusto.com
  • parameter_name:

    • 将在当前 Request 上设置的参数名称
    • 可选
    • 默认值: subdomain
  • entity:

    • 映射到子域名的实体类或Doctrine别名
    • 必需
    • 示例: Acme\DemoBundle\Entity\MySite
  • property:

    • 在您的实体中存储子域名名称的属性名称
    • 可选
    • 默认值: subdomain
  • method:

    • 将被调用的方法名称,用于获取具有定义属性的实体对象
    • 可选
    • 默认值: findOneBy

用法

  1. 创建映射到您子域名的实体
  2. 通过 Symfony Request 对象获取当前子域名对象

在 php 中,假设 netgusto_dynamic_subdomain.property 等于 subdomain(默认值)

use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller {

    public function indexAction(Request $request) {
        $subdomainobject = $request->attributes->get('subdomain');
        var_dump($subdomainobject);
    }

在 twig 中,假设 netgusto_dynamic_subdomain.property 等于 subdomain(默认值),并且映射到子域名的实体具有 title 属性

{{ app.request.attributes.get('subdomain').title }}

注意事项

如果数据库中没有找到子域名,Bundle 将抛出异常(《Symfony\Component\HttpKernel\Exception\NotFoundHttpException》)。