News

Using phpdocx with Symfony‭ ‬2.0

  • Feb 17, 2014

PHPDOCX ADVANCED AND PREMIUM LICENSES INCLUDE A NAMESPACES PACKAGE AND THE INSTRUCTIONS TO USE THE LIBRARY WITH ANY PHP COMPOSER PROJECT (Symfony 2, Symfony 3, Yii, Laravel, Drupal 8...).

THIS GUIDE IS ONLY FOR PHPDOCX 4.0 AND PREVIOUS VERSIONS.

To use phpdocx with Symfony‭ ‬2.0/2.1,‭ ‬you just need to register it in the autoloader using the UniversalClassLoader class.‭ ‬Copy the contents of the library in the vendor folder and add a new class to include phpdocx so you can instantiate it in any of your controllers.

The steps to follow are shown in more detail in what follows:

1.‏ ‎Open an autoloader.php,‭ ‬for example vendor/autoload.php,‭ ‬and add these lines of code:

  use Symfony\Component\ClassLoader\UniversalClassLoader‭;
  require_once‭ __‬DIR‭__‬.‭'‬/symfony/symfony/src/Symfony/Component/
  ClassLoader/UniversalClassLoader.php‭';
 
  $loader‏ = ‎new UniversalClassLoader‭();
  $loader-‏>‎registerPrefixes(array‭(
  'Phpdocx‏_'   =>   __‎DIR‭__‬.‭'‬/phpdocx/src‭'‬,
‏  ));
  $loader-‏>register‭();         

2.‏ ‎Copy the content of your phpdocx package into vendor/phpdocx/src/Phpdocx/src.‭ (you need to create this folder beforehand)

3.‏ ‎Inside phpdocx/src/Phpdocx create a file named Phpdocx.php and insert the following code:

  require_once‭ __‬DIR‭__‬.‭'‬/src/classes/CreateDocx.inc‭';
  class Phpdocx_Phpdocx extends CreateDocx‭ {
  }

This class allows to use phpdocx in any controller.

4.‏ ‎In your controller use phpdocx as any other external library using the class name set in the previous step.‭ ‬For example:

  /‏**
  * @Route‏("‎/content_docx/‏{‎id‭}")
  * @Template‏()
  */
  public function indexAction‭(‬$id‭)
  {
  $phpdocx‏ = ‎new‭ \‬Phpdocx_Phpdocx‭();
  $phpdocx-‏>addText‭('‬Hello‭');
  $phpdocx-‏>createDocx‭('‬/var/www/example_text‭' ‬.‭ ‬$id‭);
  }

And that is all!