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 ```php $view->setViewsDir('../app/views/'); ``` instead of ```php $view->setViewDir('../app/views/'); ``` but also docs are not accurate, when you are using ```php $view = new \Phalcon\Mvc\View(); ``` you don't have ```php public string render (string $path, [array $params]) ``` but ```php public Phalcon\Mvc\View render (string $controllerName, string $actionName, [array $params]) ``` so instead of ```php $app['view']->render('products/show', array()); ``` use ```php $app['view']->render('products', 'show', array()); ``` http://docs.phalconphp.com/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 ```php $app['view']->render(...); ``` Try ```php 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.



81.8k

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