Problem referring to a 3rd party library classes

Permalink
Hello,
I'm trying to install the dompdf library in order to use it in a single page. After two days of technical docs and forum reading, I still can't figure out what's wrong.

As suggested in old forum discussions, I installed the library with composer and than created and installed a package with the following controller.php and composer.json (cf ->https://documentation.concrete5.org/developers/packages/advanced-inc...
--------------------------------------------------------------------------------------------------------------
controller.php code

<?php
namespace Concrete\Package\Dompdf;
use Concrete\Core\Package\Package;
defined('C5_EXECUTE') or die('Access Denied.');
class Controller extends Package
{
    /**
     * The minimum concrete5 version.
     */
    protected $appVersionRequired = '8.5.4';
    protected $pkgVersion = '0.8.6';
    /**
     * The package handle.
     */
    protected $pkgHandle = 'dompdf';

--------------------------------------------------------------------------------------------------------------
composer.json code:

{
    "name" : "dompdf/dompdf",
    "type" : "concrete5-package",
    "require": {
        "concrete5/core": ">=8.5.0",
        "dompdf/dompdf": "0.8.6"
    },
    "autoload": {
        "psr-4": {
            "Dompdf\\": "src/"
        }
    },
    "config": {
        "platform": {
            "php": "7.3.22"

--------------------------------------------------------------------------------------------------------------
In my single page, I want to use dompdf classes if a form is submitted, but it never finds the class : Class 'Dompdf' not found !
the php structure of my single page looks like this :

<?php
namespace Application\Controller\SinglePage;
use Concrete\Core\Page\Controller\PageController;
use Dompdf\Dompdf;
class MySinglePage extends PageController{
   public function view(){
      // ... do some stuff
      if(isset($_POST['submit'])){
         $dompdf = new \Dompdf();
         //... do some other stuff
      }
   }
}

--------------------------------------------------------------------------------------------------------------

I would really appreciate if someone could help.

1 Attachment

 
hutman replied on at Permalink Reply
hutman
I think everything you have should work, except this line in your single page

$dompdf = new \Dompdf();


needs to be

$dompdf = new Dompdf();


because you have the use statement at the top, so you want to use the class you have included.

If that doesn't work, in your use statement add a \ to the beginning.
Junglebob replied on at Permalink Reply
Thanks for your answer, unfortunately your proposal doesn't work.

I tried "use \Dompdf\Dompdf;" and "use Dompdf\Dompdf;" and get the message "Class 'Dompdf\Dompdf' not found" when instantiating the class as proposed "$dompdf = new Dompdf();"

Any other idea ?
hutman replied on at Permalink Reply
hutman
You do have a vendor folder inside your package directory, right?
Junglebob replied on at Permalink Reply 1 Attachment
Thank you for your enlightment and your time !

The vendor folder was actually placed directly in my site root (the inscructions at the botoom of this page to install the package are quite confusinghttps://documentation.concrete5.org/developers/packages/advanced-inc...

Here's how I made it work. I hope it might help someone:

I put a simple composer-json file in my package folder
{
    "require": {
        "dompdf/dompdf": "^0.8.6"
    }
}

----------------------------------------------------------------------------------
This is the code for controller.php in the package folder
<?php
namespace Concrete\Package\Dompdf;
use Concrete\Core\Package\Package;
defined('C5_EXECUTE') or die('Access Denied.');
class Controller extends Package
{
    /**
     * The minimum concrete5 version.
     */
    protected $appVersionRequired = '8.5.4';
    protected $pkgVersion = '0.8.6';
    /**
     * The package handle.
     */
    protected $pkgHandle = 'dompdf';

----------------------------------------------------------------------------------
Using Composer, I installed the dompf library directly in the package folder(see attached picture of folder structure).

I than installed the whole package via the dasboard-->Extend Concrete5-->Add Functionnality

This is the code of my single page controller
<?php
namespace Application\Controller\SinglePage;
use Concrete\Core\Page\Controller\PageController;
use \Dompdf\Dompdf;
class MySinglePage extends PageController{
   public function view(){
      // ... do some stuff
      if(isset($_POST['submit'])){
         $dompdf = new Dompdf();
         //... do some other stuff
      }
   }
}


Many thanks to Hutman for his help!