nockieboy
nockieboy

Reputation: 367

How to access config vars from within another class in UserFrosting?

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

Upvotes: 1

Views: 90

Answers (2)

alexw
alexw

Reputation: 8688

$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.

Upvotes: 1

aslawin
aslawin

Reputation: 1981

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

Upvotes: 0

Related Questions