AKRAM EL HAMDAOUI
AKRAM EL HAMDAOUI

Reputation: 1838

How to include a file (class) in symfony2

i wanna import and view excel file using php, so i have a found a library class php-excel-reader

when we use php, we just include the file in the page using include("nameoffile.php");

i don't know how to do that using symfony. where should i import ? and how?`

Upvotes: 1

Views: 6042

Answers (2)

Cerad
Cerad

Reputation: 48865

This is for PHPExcel as opposed to the reader but the principle is the same. No need to actually include files. They can be autoloaded.

Add to your autoload.php:

$loader->registerPrefixes(array(
    'Twig_Extensions_' => $ws.'Symfony/vendor/twig-extensions/lib',
    'Twig_'            => $ws.'Symfony/vendor/twig/lib',
    'Zend_'            => $ws.'ZendFramework-1.11.11/library',
    'PHPExcel'         => $ws.'PHPExcel/Classes' // Change to support the reader
));

After which you can do something like:

$reader = new \Spreadsheet_Reader();

Note the leading slash is required to handle the non-namespaced library.

I abstracted things just a bit by using a service:

/* ==================================================
 * Wrap interface to the excel spreasheet processing
 */
namespace Zayso\CoreBundle\Component\Format;

class Excel
{
    public function newSpreadSheet()
    {
        return new \PHPExcel();
    }
    public function newWriter($ss)
    {
        return \PHPExcel_IOFactory::createWriter($ss, 'Excel5');
    }
    public function load($file)
    {
        return \PHPExcel_IOFactory::load($file);
    }
}

Upvotes: 2

Olivier Dolbeau
Olivier Dolbeau

Reputation: 1204

You can create a service which require a file. You have an example in the official documentation.

Upvotes: 0

Related Questions