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

View in Micro framework

I am trying to use views with a Micro app.

$app['view'] = function() { $view = new \Phalcon\Mvc\View(); $view->setViewDir('../app/views/'); $view->registerEngines(array( ".phtml" => 'Phalcon\Mvc\View\Engine\Volt' )); return $view; }

$app->get('/listing/{slug}', function($slug) use ($app) { echo $app['view']->render('listing_default', array( "slug" => $slug)); });

yields nothing. I have tried using the Simple view and made many changes that render nothing. The other routes I have all work fine when I simply echo content. Are there any examples of using views with Micro besides the main docs? I have copied/pasted the main Micro docs page and it still does not work for me.

Thanks!



10

$view->setViewDir('../app/views/');

Should be setViewsDir

What directory do you have your listing_default view in?

Like Victor said, use

 $view->setViewsDir('../app/views/');

instead of

 $view->setViewDir('../app/views/');

but also docs are not accurate, when you are using

 $view = new \Phalcon\Mvc\View();

you don't have

 public string render (string $path, [array $params])

but

 public Phalcon\Mvc\View render (string $controllerName, string $actionName, [array $params])

so instead of

 $app['view']->render('products/show', array());

use

 $app['view']->render('products', 'show', array());

https://docs.phalcon.io/en/latest/reference/micro.html#rendering-views they are telling about Phalcon\Mvc\View\Simple, but in code Phalcon\Mvc\View is used, althought I haven't found method to send params into view

Ok, I got it! Instead of

 $app['view']->render(...);

Try

 echo $app['view']->getRender(string $controllerName, string $actionName, [array $params]);

Thanks for the follow up. I will have to look at Phalcon again. I gave up and moved to Silex. Primarily b/c the documentation was off just a bit and I am not "seasoned" enough to determine some solutions. Silex is much slower, but I can open php files and see code worse case scenario. I do think Phalcon is a great framework! I simply might not be savvy enough for it at this point.



98.9k

Try the simple view component, maybe it works as you expect: https://docs.phalcon.io/en/latest/reference/views.html#simple-rendering