Welcome to Abdul Malik Ikhsan's Blog

Symfony2 and Zend Framework Integration

Posted in Tutorial PHP, Zend Framework by samsonasik on July 17, 2011

Symfony2 adalah Full stack PHP Development Framework dengan menggunakan fitur-fitur PHP 5.3, sedang Zend Framework adalah komponen library yang super banyak dan mempunyai layer MVC. Bila 2 Framework ini digabungkan, kebayang kan betapa dahsyatnya ? Hm…, tentu saja prinsip Use at will ( Use What you Need, Forget Everything Else) harus tetap dipakai. Kebutuhan komponen yang very komplit di Zend Framework bisa digunakan di Symfony dengan me-register-kan komponen Zend Framework pada autoloader Symfony Framewok.

Library Zend Framework yang akan kita ‘daftar’-kan di Symfony2, kita simpan dalam folder \FrameworkSymfonyApp\vendor\namapackage seperti berikut :

Nah, setelah itu, kita daftarin deh di autoloader-nya Symfony2 :

$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
   ...
));

$loader->registerPrefixes(array(
     ....
    'Zend_' => __DIR__.'/../vendor/zf'
));

Ok, selanjutnya adalah waktunya testing :D, saya akan mencoba menggunakan class Validator-nya Zend Framework, pertama, kita buat dulu class Dependency Injection-nya seperti berikut :

<?php
namespace Testmodule\DependencyInjection;

class TestDependencyContainer
{
    private $validator;

    public function __construct($validator)
    {
      $this->validator = $validator;
    }

    public function run($email)
    {
        $msg = " not valid ";
        if ($this->validator->isValid($email)) {
            $msg = " valid ";
        }

        return $msg;
    }
}

Kemudian kita coba deh panggil di Controller :

<?php
namespace Testmodule\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class TestController extends Controller
{
    /**
    * @Route("/{email}", name="_test")
    * @Template()
    */
    public function indexAction($email)
    {
        $sc = new ContainerBuilder();
        $sc->register('validatorEmail', 'Zend_Validate_EmailAddress');

        $sc->register('test','Testmodule\DependencyInjection\TestDependencyContainer')
            ->addArgument(new Reference('validatorEmail'));

        $emailisValid = $sc->get('test')->run(
               $email
          );

        return array('emailcheckvalid' => "email $email is $emailisValid ");
    }
}

Hm…, kalau berhasil, berarti tinggal ditampilkan di view seperti berikut :

{% extends "Testmodule::layout.html.twig" %}

{% block content %}
     Heiiii,... <strong><em><span style="color: red;"> {{emailcheckvalid}} </span> </em></strong>
{% endblock %}

Image :
http://www.lafermeduweb.net/billet/symfony2-les-nouveautes-resume-symfony-live-2011-1061.html

Referensi :
http://www.slideshare.net/fabpot/symfony-and-zend-framework-together-2009
http://techblog.zabuchy.net/2011/symfony-2-dependency-injection-as-a-standalone-component/
http://test.ical.ly/2011/02/24/using-zend-framework-components-in-a-symfony2-project/
http://symfony.com/doc/current/quick_tour/the_big_picture.html

2 Responses

Subscribe to comments with RSS.

  1. Abeng said, on January 19, 2013 at 4:54 am

    Aq masih bingung kang, tp tks article-nya… semoga besok2 saa bisa pakai zand-f. tks.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: