Combining Symfony and Zend Framework

During the creation of a web application, on many occasions it is necessary to do something that requires the use of an external library.

If you happen to be building your web application using symfony, it is possible to use parts of the Zend Framework (and other frameworks and libraries) independently. This enables a whole new world of features with no preset limits.

At ServerGrove we use symfony for many of our projects, and also use Zend Framework since it provides many great components. This is why, we are always up to date on both frameworks and you will get excellent technical support when hosting your framework based sites with us.

So, as an example, you can use Zend_Mail to send emails from symfony. Some other components that are very useful in the symfony world are:

- Zend_Currency
- Zend_Mime
- Zend_Ldap
- Zend_Http
- Zend_OpenId
- Zend_Pdf

And the list goes on.

But going back to the Zend_Mail example, first, we need to tell symfony where to find the Zend Framework library.

All our servers here at ServerGrove come with not only symfony pre-installed but also we have all latests Zend Framework releases. It will be located at /usr/local/php/Zend. This will always be the latest stable version. If you need to use a specific version of the framework (to avoid possible future compatibility issues) you can also link to a specific version, for example /usr/local/php/ZendFramework-1.7.0/library

So, we need to modify the file config/ProjectConfiguration.class.php so we can use the autoloader provided by ZF, it should look like this:


// config/ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
static protected $zendLoaded = false;

static public function registerZend()
{
if (self::$zendLoaded)
{
return;
}

$zflib = '/usr/local/php/Zend';

set_include_path($zflib.PATH_SEPARATOR.get_include_path());
require_once $zflib.'/Loader.php';
Zend_Loader::registerAutoload();
self::$zendLoaded = true;
}

// ...
}

Then when we need to send the email, usually in an action, this is what we do:


ProjectConfigation::registerZend();
$mail = new Zend_Mail();
$mail->setBodyText('Message body');
$mail->setFrom('me@example.com', 'My name');
$mail->addTo($recipientEmail);
$mail->setSubject('Subject');
$mail->send();

That’s it. Could not be simpler! As we said, this opens many possibilites by using external libraries and avoids reinventing the wheel. By using the set of libraries and frameworks we provide on ServerGrove, it has never been easier to develop and run powerful and professional websites.

7 Comments Add Yours ↓

The upper is the most recent comment

  1. 1

    Hi,

    As zend has an autoloader you can integrate it as it’s explained on symfony website : http://www.symfony-project.org/book/1_2/17-Extending-Symfony#Integrating%20with%20Other%20Framework‘s%20Components

    You might have noticied that’s possible, and if you did, I’ll be interested to know why you don’t

  2. 2

    That’s the other way of doing it. We actually use this one you are recommending in a lot of places. But for small and occasional utilization of the Zend Framework is better to register it only when you need it so you don’t get the performance hit all the time.

    thanks for your comment!

  3. 3

    Zend and Symfony mix well together. Also, I need some help with Zend Lucene in Symfony… I would like to index several object type and then search in the index. I have found no documentation about this. Any clue ?

  4. 4

    Yes, Zend Lucene is very interesting. We have considered (and still are) for a project we are working on. We will comment on it if we get to it. There are several posts about using it, check in google.

  5. yunior #
    5

    I ca do the same, if I want the zen session for my aplication in symfony?

  6. 6

    I notice that this site is built in Zend Framework; in your (expert :) opinion which is the better framework to build a complex web application in? ZF or Symfony?

    Would it be possible to do a comparison, or pro’s and con’s between the two frameworks, something which I know would be really usefull to me, and I guess many other people?

  7. 7

    @Chris

    Our site is mainly developed using symfony. We use Zend Framework quite a lot too. We like symfony better, due to its ORM integration, CLI tools (ZF got CLI tools just recently while symfony has it for quite some time), and admin generator, they save so much time. Zend Framework has a very nice set of components that we use independently and integrated to symfony. Both are great frameworks and have a very promising future as their growing popularity is showing.




Your Comment


blog comments powered by Disqus