rc/magento-bundle

将 Magento 集成到 Symfony2 2.3.* 应用程序中

安装: 11

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 16

类型:symfony-bundle

dev-master 2013-11-02 12:25 UTC

This package is not auto-updated.

Last update: 2024-09-10 05:00:36 UTC


README

将 Magento 集成到 Symfony2 应用程序中。

此 Bundle 仍在开发中,但目标是能够在 Symfony 内部与 Magento 进行通信。这意味着 Magento 应用程序在 Symfony2 内部初始化,以共享单个会话、启用在 Symfony2 中重用 Magento 登录以及从 Magento 读取内容和布局。

安装

  1. 在您的 deps 文件中添加以下行

    [LiipMagentoBundle]
        git=http://github.com/liip/LiipMagentoBundle.git
        target=/bundles/Liip/MagentoBundle
    
  2. 运行 vendors 脚本

    $ php bin/vendors install
    
  3. 将 Liip 命名空间添加到您的自动加载器中

    // app/autoload.php
    $loader->registerNamespaces(array(
        'Liip' => __DIR__.'/../vendor/bundles',
        // your other namespaces
    ));
    
  4. 将 Bundle 添加到您的应用程序内核中

    // app/AppKernel.php
    public function registerBundles()
    {
        return array(
            // ...
            new Liip\MagentoBundle\LiipMagentoBundle(),
            // ...
        );
    }
    
  5. 配置 Bundle

    配置

  6. 修复 Magento 自动加载器

    $ cd $MAGENTO_DIR
    $ patch -p0 < $SYMFONY_DIR/vendor/bundles/Liip/MagentoBundle/magento-autoloader.patch
    

配置

# app/config/config.yml
framework:
    session:
        # use the Magento session handler
        storage_id: liip_magento.session.storage

liip_magento:
    # path to the Mage.php file
    mage_file:  %kernel.root_dir%/../../magento/app/Mage.php
    # not for all store resolvers, mapping to store code
    store_mappings:
        de: de
        en: en
        fr: en
# app/config/security.yml
security:
    factories:
        - "%kernel.root_dir%/../vendor/bundles/Liip/MagentoBundle/Resources/config/security_factories.xml"

    providers:
        magento:
            id: security.user.provider.magento

    firewalls:
        secured_area:
            pattern:    ^/
            anonymous: ~
            magento:
                provider:   magento
                check_path: /login_check
                login_path: /login
            logout:
                path:   /logout
                target: /

用法

存储解析器

为了访问客户数据,我们需要初始化正确的 Magento 店铺。Magento 加载默认的店铺,该店铺可以配置为相应的组。您可以去 系统 > 管理店铺 然后打开“店名”行中的项目(实际上是组)并选择“默认商店视图”。

存储解析器确定要初始化哪个 Magento 店铺。每次它无法确定店铺时,它都会保留默认的店铺。此外,如果它无法设置解析的店铺,它将回到默认店铺。

默认存储解析器是 LocaleStore,它使用 symfony 本地化作为店铺代码。如果您的店铺代码与本地化不匹配,则需要使用 LocaleStoreResolver,它允许您通过 store_mappings 配置本地化与店铺代码的映射。这也有助于您根据本地化需要多个回退店铺。

对于更定制的解析器,您也可以通过实现 StoreResolverInterface 来编写自己的。在这种情况下,默认解析器服务可以按照以下方式重写

# app/config/config.yml
liip_magento:
    service:
        store_resolver: my.store_resolver.id

从 Magento 获取数据和 HTML

在这个演示中,我们加载了页脚块和 Magento 购物车中的项目数量

class MagentoController extends Controller
{
    /**
     * @Template()
     */
    public function indexAction()
    {
        $block = \Mage::getSingleton('core/layout');
        $footer = $block->createBlock('page/html_footer');
        $footer->setTemplate('page/html/footer.phtml');

        $cart = \Mage::helper('checkout/cart')->getCart()->getItemsCount();
        return array('cart' => $cart, 'footer' => $footer->toHTML());
    }
}

演示的模板片段

{% block content %}
    
    You have {{ cart }} items in your cart!
    
    {{ footer | raw}}
{% endblock %}  

监听 Magento 事件

使用Magento Symfony 模块,您可以在您的 Symfony 应用程序中监听 Magento 发射的事件。

以下是一个处理 customer_address_save_after 事件的示例,例如,将您的 CRM 后端与 Magento 客户同步

在 Symfony 中注册您的监听器

services:
    acme_demo_bundle.customer_save_after: 
        class: %acme_demo_bundle.customer_save_after.class%
        arguments: [ @some_service_id ]
        tags:
            - { name: kernel.event_listener, event: mage.customer_save_after, method: synchronize }

在 Magento 中分发事件

<?xml version="1.0"?>
<config>
    <modules>
        <MyModule_Core>
            <version>0.1.0</version>
        </MyModule_Core>
    </modules> 
    <global>    
      <events>
          <customer_address_save_after>
              <observers>
                  <address_update>
                      <type>singleton</type>
                      <class>MyModule_Core_Customer_Synchronizer</class>
                      <method>synchronize</method>
                  </address_update>
              </observers>
          </customer_address_save_after>
      </events>
    </global>
</config>
<?php 
class MyModule_Core_Customer_Synchronizer
{
    
    public function synchronize(Varien_Event_Observer $observer) 
    {                
        $mageEvent = $observer->getEvent();
        $symfonyEvent = new MageEvent($mageEvent);        
        $container = Mage::getSingleton('Symfony_Core_DependencyInjection_Container');
        $container->get('event_dispatcher')->dispatch('mage.customer_save_after', $symfonyEvent)        
        
    }
}

更多配置示例

示例 1 - 在子域名上运行 magento

  • 创建 2 个虚拟主机,一个用于 Symfony,一个用于 Magento,例如 mysite.localshop.mysite.local
Apache example:

<VirtualHost *:80>
    DocumentRoot "/var/www/mysite/symfony/web"

    ServerName mysite.local

    <Directory "/var/www/mysite/symfony/web">
        AllowOverride All
    </Directory>

</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/mysite/magento"

    ServerName shop.mysite.local

    <Directory "/var/www/mysite/magento">
        AllowOverride All
    </Directory>

</VirtualHost>
  • 设置 Magento(例如在 ../project/magento),并配置 cookie 路径为 / 和 cookie 域为 .local
  • 设置 Symfony(例如在 ../project/symfony)和 LiipMagentoBundle
  • 添加 loginlogin_check 路由并设置登录表单,请参阅Symfony 文档

之后,您应该在 mysite.localshop.mysite.local 之间同步会话,这意味着在任一侧登录/注销都会在另一侧登录/注销用户。

示例 2 - 将 magento 作为别名运行(例如 mysite.com/shop)

  • 创建 1 个虚拟主机并为 Magento 添加别名
Apache example: 

<VirtualHost *:80>
    DocumentRoot "/var/www/mysite/symfony/web"

    ServerName mysite.local

    Alias /shop /var/www/mysite/magento

    <Directory "/var/www/mysite/symfony/web">
        AllowOverride All
    </Directory>

    <Directory "/var/www/mysite/magento">
        AllowOverride All
    </Directory>
</VirtualHost>

  • 设置 Magento(例如在 ../project/magento),并配置 cookie 路径为 / 和 cookie 域为 .local
  • 设置 Symfony(例如在 ../project/symfony)和 LiipMagentoBundle
  • 添加 loginlogin_check 路由,并设置登录表单,请参考 Symfony 文档

之后,你应该在 mysite.localmysite.local/shop 之间同步了会话,这意味着在任一侧登录/注销都会在另一侧登录/注销用户。