arthur arthur - 5 months ago 52
PHP Question

Phalcon Micro Application adding views

I started building a rest API and went with a Phalcon micro application.

Now I'd like to add 1 view, pretty much an interface to interact with the API rather than having to look through JSON.

I'm new to phalcon and not sure how to pass a parameter to a view when I'm using a phalcon micro application. I just need something simple as so:

$app->get('/', function () {
//some database query
//pass variable to views/index.phtml
require 'views/index.phtml';


You can do this easily by loading a simple view into your DI

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

And then use it to load your view.

$app->get('/', function () use ($app) {
    // other logic
    echo $app['view']->render('index', ['key' => 'value']);

The first parameter in $app['view']->render takes the location of your view (inside the defined setViewsDir). The second parameter is an array to assign variables to your view.