1

can anyone give me a quick hand with namespaces and the UserFrosting environment?

To explain my issue, I'm using TCPDF to create a PDF document from data within UserFrosting. I've created a custom class, MyPDF, in the UserFrosting namespace so I can create a PDF within the UF namespace by typing $pdf = new MyPDF(blahblah); and that works fine.

The issue is with the MyPDF class itself - I need to be able to reference config vars from UF and don't know how I can do that - i.e.:

namespace UserFrosting;

 class MyPDF extends \TCPDF_TCPDF {

   public function Header() {
      $image_location = $this->_app->config('upload.path')

How can I access config from within MyPDF? :?:

I've tried:

class MyPDF extends \TCPDF_TCPDF {

 public function Header() {
    $ufapp = new UFModel();
    $image_location = $ufapp->config('upload.path')

... but no dice. I'm getting this error:

Cannot instantiate abstract class UserFrosting\UFModel
nockieboy
  • 347
  • 1
  • 3
  • 14

2 Answers2

1

$app is just the global instance of UserFrosting that the entire application runs on. Since UserFrosting extends Slim\Slim, you can access it statically using the getInstance() method:

$app = UserFrosting::getInstance();

A better way, though, would be to actually pass $app into your MyPDF constructor. However, depending on your situation and where else you are using MyPDF, that might be more difficult.

alexw
  • 8,468
  • 6
  • 54
  • 86
0

It's written in error: you can't instantiate a abstract class. Create class which extends from UFModel and implement all abstract methods from this class.

For example, if your UFModel class looks like this:

<?php

abstract class UFModel
{
   abstract public function getValue();
}

you should create class which extends this class and implements all abstract methods:

<?php

class MyModel extends UFModel
{
   public function getValue()
   {
       return 'exampleValue';
   }
}

Now you can create object using new operator:

<?php

//...
$ufapp = new MyModel();
//...

More about abstract classes in PHP

aslawin
  • 1,981
  • 16
  • 22
  • But MyPDF class needs to extend TCPDF_TCPDF. MyPDF is being instantiated within a class that extends UserFrosting\BaseController class - `$pdf = new MyPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);` Hm.. I'm confused as hell by all this, sorry if the question isn't clear.. :( – nockieboy Sep 06 '16 at 09:30
  • But error is about instantiate `UFModel`, not `MyPDF` class. – aslawin Sep 06 '16 at 10:12
  • Yeah, sorry about that - I was just trying to find a solution and groping in the dark with that line. The issue is that I need to access $app, which is the instance of the class I'm calling `new MyPDF()` from. Without that, I can't access the `config[]` vars within the MyPDF class. – nockieboy Sep 06 '16 at 10:24
  • Actually, I could modify the parameters taken by the MyPDF constructor to take `$app` as an argument? – nockieboy Sep 06 '16 at 10:25