Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Undefined Variable

I am trying to get volt setup in phalcon. I used the dev tools to create the project. I tried to pass in a variable using both methods, the magic method and the setVar() method.

$posts = new Posts();
$posts->title = "asdf";
$this->view->posts = $posts; 
$this->view->setVar("posts", $posts);

Then in my index.volt file under ../views/posts/index.volt

{% if ((posts is empty))  %}
{% endif %}
<p>{{ posts.title }}<p>                 # <- THIS IS THE TROUBLED CODE
<p>You're now flying with Phalcon. Great things are about to happen!</p>

It displays:

What am I doing wrong? Any help is apreciated.




Ok, so the top part of your code is going on in the controller?

Lose the $this->view->setVar("posts", $posts); part! And also lose the brackets around ((posts is empty)). And change your syntax. Just use {% if not posts %}


edited Oct '14

Thanks, but I was showing both of the methods that I have tried to use. Currently there is $this->view->posts = $posts;.


You're talking aboutt volt here. So losing one of those methods in your controller doesn't count here as far as this question is concerned.

If you edit your .volt-file: are you seeing the expected results? If not, then what do you see? Help us help you dude... thanks!

I attached the screenshot in the original post below the code. Thanks.

This is the Setting up the view component in services.php that theoretically allows me to use volt. (Correct?)

    $di->set('view', function () use ($config) {

    $view = new View();


        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'

            return $volt;
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'

    return $view;


Hey I think you should upload full source on github, maybe I will help you

Hey here's the link, thanks.

edited Oct '14


I don't see you code in indexAction, you should look like

 public function indexAction()
  $posts = Posts::find();
  $this->view->setVar('post', $posts);

Then in view(index.volt):

{% for item in post %}

I am using mongo, but I am trying to get this to work without it. So that I know that it is not my mongo connection that is failing. That is why I created the $test Post. The current code:

   public function indexAction()
        #$posts = Posts::find(array(array(), "limit" => 10));
        $test = new Posts();
        $test->title = "asdf";
        $this->view->setVar('post', $test);

If I add my code to the indexAction() no html renders, instead I get these error boxes:

  • Undefined property: PostsController::$view in /Users/Stephen/Sites/blog_v2/blog/app/controllers/PostsController.php

  • Fatal error: Call to a member function setVar() on a non-object in /Users/Stephen/Sites/blog_v2/blog/app/controllers/PostsController.php on line 12.

The stacks are the same for both

####### Time Memory Function Location

1 0.0001 240104 {main}( ) ../index.php:0

2 0.0005 274112 handle ( ) ../index.php:27

3 0.0007 321120 PostsController->indexAction( ) ../index.php:0

Thanks for the help.


Hey I dont know why this, but you can test simple, like this

   public function indexAction()
        #$posts = Posts::find(array(array(), "limit" => 10));

        $this->view->setVar('post', 'This is test');

this is working yet

I tried that, and adjusted the volt file to say {{ post }}. I still get the same errors as the previous post.

edited Oct '14

well, i see problem of you that is controller Post

class PostsController extends \Phalcon\Mvc\Collection

Change to

class PostsController extends \Phalcon\Mvc\Controller

I test that ok, note that \Phalcon\Mvc\Collection used to Model not controller

That did it. I think I accidentally changed the PostsController extends MVC\Controller to MVC\Collection instead of changing the Model. I thought I was going insane. Thanks!