Symfony2 - use a third-party library (SSRS)

Perhaps a dumb question, I'm new to Symfony2, and I'm using it for one of my projects.
I would like to be able to use a third-party library, namely SSRSReport (API for SSRS reporting).

I put the library in Symfony/vendor/ssrs/lib/Ssrs/src .
There are many classes here, I do not need them to be automatically loaded.

I just don't know how to request and call them from the controller.

Of course, this does not 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 the many classes contained in the API.

+4
source share
2 answers

First of all, I do not recommend embedding libraries that do not support symfony in /vendors . Since you manage this library, put it in /src .

Secondly, when using classes that are not a namespace (i.e., located in the root namespace), make sure that you reference them correctly, otherwise PHP will look in the current namespace (which in this case is your namespace controllers)

Thirdly, a quick and dirty solution is to simply include files from the controller correctly:

 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 this blocks your logic for including the library in this one controller. You can create a separate shell for the SDK that does this, or even register it as a service.

+4
source

You might be using composer with symfony, so this is my suggestion.

Instead of require_once, you should use the composer's autoload mechanism to automatically load libraries or functions without names, http://getcomposer.org/doc/04-schema.md#files

So just update the startup section in composer.json.

  "autoload": {
         "psr-0": {"": "src /"},
         "files": ["src / SsrsReport / SSRSReport.php"]
    },

To use the service, I would either use Facade (extends the SSRSREport class) or Factory, which returns it.

+1
source

Source: https://habr.com/ru/post/1415984/


All Articles