ekino/drupal-bundle

集成Symfony2与Drupal

安装: 526

依赖: 0

建议者: 0

安全: 0

星标: 72

关注者: 33

分叉: 20

开放问题: 4

类型:symfony-bundle

2.0.0 2020-01-06 15:34 UTC

This package is auto-updated.

Last update: 2024-09-07 01:43:15 UTC


README

Build Status

要求至少Drush 5.0以与Symfony控制台兼容。

此捆绑包尝试深度集成Symfony2与Drupal,以及Drupal与Symfony2。当然,这是在不修改Drupal核心的情况下完成的。

当此捆绑包被激活时,Symfony2控制台将自动加载Drupal库。因此,它使得从您的Symfony2命令中使用Drupal库成为可能。

安装

下载symfony2沙盒和Drupal代码

安装文件以获得以下结构

Symfony Sandbox Root
  - app
  - vendor
  - src
  - web (Drupal source code)

目录web必须是文档根目录,并包含Drupal源代码。

更新index.php文件

此文件与Drupal“共享”容器,因此可以在Drupal中使用Symfony2的服务。初始化过程始终由Symfony2处理。

<?php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/bootstrap_cache.php.cache';
//require_once __DIR__.'/../app/AppCache.php';

use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel('dev', true); //
$kernel->loadClassCache();
$kernel->boot();

// make the Symfony container available from Drupal file
global $container;

$container = $kernel->getContainer();

$request = Request::createFromGlobals();

$response = $kernel->handle($request);
$response->send();

$kernel->terminate($request, $response);

安装相关Drupal模块

可以从以下url下载该模块:https://github.com/ekino/ekino_drupal_symfony2

配置

编辑Symfony config.yml文件并添加以下行

parameters:
    session.flashbag.class:       Ekino\Bundle\DrupalBundle\Port\DrupalFlashBag
    session.attribute_bag.class:  Ekino\Bundle\DrupalBundle\Port\DrupalAttributeBag

framework:
    # ... configuration options
    session:
        # ... configuration options
        storage_id:     ekino.drupal.session.storage

ekino_drupal:
    root:          %kernel.root_dir%/../web
    logger:        ekino.drupal.logger.watchdog
    strategy_id:   ekino.drupal.delivery_strategy.symfony
    # attach a security token to the following provider keys
    provider_keys: [main, admin]

    # not required
    entity_repositories:
        # 3 equivalent examples of configuration:
        - { bundle: page }
        - { type: node, bundle: page }
        - { type: node, bundle: page, class: Ekino\Bundle\DrupalBundle\Entity\EntityRepository }
        # you can also define an entity repository:
        - { type: node, class: Application\Ekino\Bundle\DrupalBundle\Entity\Node\NodeRepository }

    # switch to true if you want to prefix the name of Symfony tables
    table_prefix:
        enabled: false
        prefix:  symfony__
        exclude: [users]

    # optional
    session:
        refresh_cookie_lifetime: true # default value: false

# declare 2 required mapping definition used by Drupal
doctrine:
    dbal:
        driver:   %database_driver%
        dbname:   %database_name%
        user:     %database_user%
        host:     %database_host%
        port:     %database_port%
        password: %database_password%
        charset:  UTF8

        mapping_types:
            longblob: object
            blob: object

        # Tips: this allows Doctrine to consider only tables starting with
        # "symfony__" during a migration generation.
        # Think to add Doctrine migrations table here or configure it in
        # the doctrine_migrations section (table_name)
        schema_filter: ~^(symfony__|migration_versions)~

该捆绑包附带3种交付策略

  • ekino.drupal.delivery_strategy.background: Drupal从不返回响应,Symfony返回
  • ekino.drupal.delivery_strategy.drupal: Drupal始终返回响应,即使页面是404
  • ekino.drupal.delivery_strategy.symfony: Drupal只有在页面不是404时才返回响应

(可选)部分 entity_repositories 允许您轻松与Drupal API交互以检索内容,并在Symfony代码中处理。配置提供默认值

  • 默认实体类型是 node
  • 默认存储库类是 Ekino\Bundle\DrupalBundle\Entity\EntityRepository,请随意配置您的

更新查询

UPDATE users SET `emailCanonical` = `mail`, `usernameCanonical` = `name`, `roles` = 'b:0;';

用法

Symfony组件可以在Drupal中使用

<?php
function drupal_foo_function() {
    $result = symfony_service('reusage_service')->foo();

    // do some stuff with $result
}

安全

您可以使用Drupal权限来保护Symfony路由,前缀为PERMISSION_DRUPAL_。例如

security:
    role_hierarchy:
        # ...

    firewalls:
        # ...

    access_control:
        - { path: ^/symfony/admin, role: PERMISSION_DRUPAL_ACCESS_ADMINISTRATION_PAGES }

PERMISSION_DRUPAL_ACCESS_ADMINISTRATION_PAGES翻译为“访问管理页面”并与user_access和全局Drupal用户一起使用。

如果您想使用“个人访问”权限,例如,使用role PERMISSION_DRUPAL_PERSONAL_ACCESS。

限制

  • 无法使用Symfony原生类来管理会话,因为Drupal初始化了它自己的会话处理器,并且无法更改此设置。
  • 请求必须通过index.php提供,因为这是.htaccess文件中的默认值,并且无法更改Drupal中的默认脚本。

预览

如果安装成功完成,欢迎页面看起来像

Screenshot

您可以在底部看到Symfony Web Debug Toolbar ;-)。