Jonathan Nock Jonathan Nock - 2 months ago 8
PHP Question

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

Answer

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

Comments