liip/magento-bundle

此软件包已被放弃,不再维护。未建议替代软件包。

将 Magento 集成到 Symfony2 应用程序中

安装: 123

依赖关系: 0

建议者: 0

安全: 0

星星: 69

关注者: 54

分支: 16

开放问题: 3

类型:symfony-bundle

dev-master 2017-12-05 09:20 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:20:47 UTC


README

此软件包不再维护。如果需要,请随意进行分支。

MagentoBundle

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

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

安装

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

    [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. 将软件包添加到您的应用程序内核

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

    配置

  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 之间同步会话,这意味着在任何一侧登录/注销都会在另一侧登录/注销用户。