We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

"Phalcon Exception: Views directory must be a string or an array", but it is a string...(?)

Hi there

The "echo" show me C:\Apache24\htdocs\phalcon\app\views on my screen, but phalcon says it's not a string. But it is a string in my opinion!!

$APP_PATH_VIEWS = "".APP_PATH . SEPARATOR.'views'; echo 'APP_PATH_VIEWS: '."\r\n".$APP_PATH_VIEWS."\r\n\r\n";

    $di = new Phalcon\DI\FactoryDefault();

    $di->set('view', function() {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir($APP_PATH_VIEWS);
        return $view;
    });

And that does also not work: $view->setViewsDir("" . $APP_PATH_VIEWS);

Thank you for your feedbacks.

With best regards, Jan

How to format code on that forum? Can't see any suitable options in the toolbar...

Oh it was that what was missing, so the problem is solved now: global $APP_PATH_VIEWS;

Regards, Jan



125.7k
Accepted
answer
edited Oct '19

If an answer (even yours) has been accepted, please click the "Accept Answer" button. That will update the post title and let others know the problem has been solved.

Using global is generally frowned upon, because doing so provides no insight as to where that variable is defined. If that variable is defined in the same file and context as the code you posted above, you can use the use directive to import variables from the scope. For example:

$APP_PATH_VIEWS = APP_PATH . SEPARATOR.'views';// removed the leading "", as it's unnecessary.

$di = new Phalcon\DI\FactoryDefault();

$di->set('view', function() use($APP_PATH_VIEWS) {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir($APP_PATH_VIEWS);
        return $view;
});

Thanks!! :-)

Regards, Jan

Don't ever use globals, forget this keyword even existis, you better stick to consts for example.