16.08.11 - Add third party libraries to Symfony 2

How to add third party libraries to our Symfony 2 project

Adding libraries that follows the PHP 5.3.+ with namespaces is trivial, but how we can add an old PHP library without namespaces?

For example, if we want to add a library like GeSHi which haves an old style format (lower than 5.3.+), not follows the standards and it not have namespaces, we have to do a little trick.

As the Symfony 2 standard says, we have to store all the external libraries into the /vendor folder. In order to force GeSHi to load like the PSR-0 standard (fact standard for autoloading in Symfony 2) we must create the following directory structure:
/vendor/geshi/lib/Geshi/src
And store there the GeSHi lib.

Now we have to make an special class to load GeSHi (class which inherits from real GeSHi class):
<?php
# vendor/geshi/lib/Geshi/Geshi.php

require_once __DIR__.'/src/geshi.php';

class Geshi_Geshi extends GeSHi {
}
Now we can add safety the GeSHi lib to the autoloader:
# app/autoload.php

$loader->registerPrefixes(array(
    'Geshi_' => __DIR__.'/../vendor/geshi/lib',
));
Finally we can use it everywhere in our Symfony 2 project:
$geshi = new \Geshi_Geshi ();
More news about:  Symfony2
Tags:  php,  geshi,  Symfony2,  autoload,  namespaces

Comments

  • Gravatar
    22.08.11 - 10:59  Jedix

    Very interesting thank you. Do we have to extends every class of the library ??

  • 19.10.11 - 04:22  kiwwito
    No, only the ones that you're going to use directly (create new instances), normally the main one ;)
  • Gravatar
    19.10.11 - 02:06  m47730

    Great! Simple and effective! thank you to explain in so simple way how to include external libraries!

  • Gravatar
    23.01.12 - 10:59  Alberto Gaona

    I was looking for a way to solve this all morming. Thank you very much

  • Gravatar
    30.01.12 - 02:48  Adam Knowles

    This really helped me, was struggling with how to integrate a 3rd party PHP class into Symfony2. Followed your instructions exactly and it seems to work! Huge thanks!

  • Gravatar
    13.02.12 - 10:40  Fernando

    i have problems to load a simple class cold sfEncrypt.php sfEncrypt { function myfunction() { ..... } ........ } The directory structure is vendor/sfEncrypt/lib/SfEncrypt/src the lib is under /src SfEncrypt/Sfencrypt.php: require_once __DIR__.'/src/sfEncrypt.php'; class SfEncrypt_SfEncrypt extends sfEncrypt { } in autoload.php $loader->registerPrefixes(array( 'SfEncrypt_ ' => __DIR__.'/../vendor/sfEncrypt/lib' )); in controller: $sfEncrypt = new \SfEncrypt_SfEncrypt(); i get: Fatal error: Class 'SfEncrypt_SfEncrypt' not found ????

  • 15.02.12 - 10:12  kiwwito
    You must follow the same naming convention for all the code. PHP differentiates upper and lower cased characters. Is not the same "sfEncrypt" than "SfEncrypt" than "sfencrypt". You must be clear :)
  • Gravatar
    03.03.12 - 06:09  Alex

    Thank You very much! It works with Facebook lib :)

  • Gravatar
    30.06.12 - 06:51  kakashi152

    Thanks for the solution, it just works!

  • Gravatar
    16.11.12 - 08:14  north face osito jacket

    Awesome blog! Do you have any helpful hints for aspiring writers? I'm hoping to start my own website soon but I'm a little lost on everything. Would you recommend starting with a free platform like Wordpress or go for a paid option? There are so many choices out there that I'm totally overwhelmed .. Any ideas? Many thanks!

  • 03.12.12 - 01:43  kiwwito
    I use a custom framework made by my own. There's a lot of stuff out there, a good choice could be Tumblr, Blogspot or Wordpress :)

Comment

Your comment was sended successfully. In due to filter SPAM, incorrect, harmful or uncivilized comments, your message is waiting for moderation. Once it passes it will be published. Thanks.