E-Commerce solution based on Symfony 2 components

By Manuel Raynaud / @manuraynaud

AFUP Lyon 03/04/2015

THELIA

  • Framework e-commerce
  • Stable since a year
  • Used in many projects
  • Lots of features

Thelia is not

Yet Another Symfony Ecommerce Application

  • Created in 2005
  • Only in french (cocorico !)

Why Thelia 2 ?

  • Modern code
  • Code and Doc in English
  • New features

Symfony components

  • Dependency Injection
  • Event Dispatcher
  • Http Kernel
  • Http Foundation
  • Form
  • Console

Symfony CMF component

  • Routing

Version 2

https://github.com/thelia/model

http://raynaud.io/2015/03/30/why-we-choose-smarty-in-thelia.html

continuous integration

  • Third party dependencies
  • Manage Thelia's module

Install

                        
$ composer create-project thelia/thelia-project myProject 2.1.2
$ cd myProject
$ php Thelia thelia:install
                        
                    

Modules

Structure

                    
MyModule
    MyModule.php
    Config
        config.xml
        module.xml
        routing.xml
        schema.xml
    Controller
        MyModuleAdminController.php
    Hook
        MyModuleHooks.php
    I18n
        fr_FR.php
        en_US.php
    Model
        Base
        Map
        MyModule.php
        MyModuleQuery.php
    Listeners
        CartListener.php
        OrderListener.php
    Loop
        MyModuleLoop.php
    templates
        ...
                        
                    

Main Concept

Event-driven programming paradigm

How to dispatch an event

Retrieve the dispatcher

                        
//From the container
$eventDispatcher = $container->get('event_dispatcher');

//From an event
$eventDispatcher = $event->getDispatcher();
                        
                    

Create an event

1 event = 1 object

                        
<?php
namespace Thelia\Core\Event;
use Thelia\Core\Event\ActionEvent;
class Cart extends ActionEvent
{
    protected $productId;
    protected $quantity;

    public function __construct($productId, $quantity)
    {
        $this->productId = $productId;
        $this->quantity = $quantity;
    }

    public function getProductId()
    {
        return $this->productId;
    }

    public function getQuantity()
    {
        return $this->quantity;
    }
}
                        
                    

Dispatch !

                        
use Thelia\Core\Event\Cart;
use Thelia\Core\Event\TheliaEvents;

//

$eventDispatcher = $container->get('event_dispatcher');
$cartEvent = new Cart(1, 3);
$eventDispatcher->dispatch(TheliaEvents::CART_ADD, $cartEvent);
                        
                    

Events in Thelia

Create a listener

                    
<?php
namespace MyModule\Listener;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Event\CartEvent;
Class CartListener implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [
            TheliaEvents::CART_ADD => ['addItem', 100]
        ];
    }

    public function addItem(CartEvent $event)
    {
        $item = $event->getCart()->getLast();

        $item->addQuantity(rand(1,5));
        $item->save();
    }

}
                        
                    

Declare a listener

                        
<service id="MyModule.cart.listener" class="MyModule\Listener\Cart">
    <tag name="kernel.event_subscriber"/>
</service>
                        
                    

Pros

  • Can choose the priority
  • Can skip Thelia default behaviour

Cons

Event propagation can be stopped

Routing

routing.xml file in Config folder

                        



    
        
            MyModule:MyModuleAdmin:foo
        
    

                        
                    

Custom router

                        

    
    MyModule/Config/custom_routing.xml
    
        %kernel.cache_dir%
        %kernel.debug%
    
    
    

                        
                    

Loop

                    
{ifloop rel="loop_id"}
    

Display if loop returns at least 1 result

{loop id="loop_id" type="product" arg1="value1" arg2="value2"} price : {$PRICE} {/loop}

Display if loop returns at least 1 result

{/ifloop} {elseloop}

Display if loop returns nothing

{elseloop}

TheliaStudio

Module generator

Generates

  • CRUD
  • Configuration
  • Forms
  • Loops

Showcase

Thelia + Symfony 2 + Arduino = <3

Thelia + Symfony 2 + Mongo DB = <3

Full Thelia + irec

Demo

Demo

Demo Back-office

Conclusion

  • Easy to learn
  • Easy to use
  • Highly customizable
  • Use only what you need

Thanks !

http://thelia.net

http://demo.thelia.net

http://doc.thelia.net

https://github.com/thelia

@theliaecommerce