Symfony2 - Use a third party library (SSRS)

php, reporting-services, symfony

Solution

First of all, I don't recommend putting non-symfony-managed libraries into `/vendors`. Since you're managing this library, put it into `/src`.

Secondly, when using classes that aren't namespace (i.e., are in the root namespace), make sure you reference them properly or else PHP will look in the current namespace (which, in this case, is your controller namespace)

Thirdly, the quick-and-dirty solution is to just properly include the files from the controller:

class DefaultController extends Controller
{
    protected function includeSsrsSdk()
    {
      require_once(
          $this->container->getParameter( 'kernel.root_dir' )
        . '/../src/ssrs/lib/Ssrs/src/SSRSReport.php'
      );
    }

    public function viewAction()
    {
        $this->includeSsrsSdk();
        define("UID", "xxxxxxxx");
        define("PASWD", "xxxxxxxx");
        define("SERVICE_URL", "http://xxx.xxx.xxx.xxx/ReportServer/");
        $report = new \SSRSReport(new \Credentials(UID, PASWD), SERVICE_URL);
        return $this->render('myBundle:Default:view.html.twig'
            , array('report' => $report)
        );
    }
}

But that locks your logic for including the library into this one controller. You could make a separate wrapper for the SDK that does this, or even register it as a service.

Problem

Maybe dumb question, I'm new to Symfony2 and I'm using it for one of my projects. I'd like to be able to use a third party library, namely SSRSReport (an API for SSRS Reports). I have put the library into `Symfony/vendor/ssrs/lib/Ssrs/src`. There are many classes defined here, I don't need them to be autoloaded. I simply don't know how to require and call them from a controller. For sure this doesn't work ``` require_once '/vendor/ssrs/lib/Ssrs/src/SSRSReport.php'; class DefaultController extends Controller { public function viewAction() { define("UID", "xxxxxxxx"); define("PASWD", "xxxxxxxx"); define("SERVICE_URL", "http://xxx.xxx.xxx.xxx/ReportServer/"); $report = new SSRSReport(new Credentials(UID, PASWD), SERVICE_URL); return $this->render('myBundle:Default:view.html.twig' , array('report' => $report) ); } } ``` `SSRSReport()` and `Credentials()` used here, are 2 of many classes contained into the API.

Original source