September 01, 2014

September 01, 2014
In this article, we are going to discuss about How to integrate Zend framework components with Symfony2 framework. Symfony2 is a full stack of PHP Development Framework by using the features of PHP 5.3. Zend Framework is a component library that is super a lot and have a MVC layer. When 2 Framework is incorporated, imagine how powerful right?

Hm ... of course the principle of use at will (Use What you Need, Forget Everything Else) should still be used. The need for very complete component in Zend Framework can be used in Symfony with me registers the Zend Framework components in Symfony autoloader Framewok.


Zend Framework library that will we 'list'-right in Symfony2, we have saved in the folder \FrameworkSymfonyApp\vendor\namapackage as follows:

Well, after that, we document in its Symfony2 autoloader:

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

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

Ok, next is testing time. I will try to use his class Validator Zend Framework, first, we make the first of his class Dependency Injection as follows:

<?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;
    }
}

Then we try to call on 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 ");
    }
}

Hmm, if successful, its staying in the view as shown below:

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

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

0 comments:

Post a Comment