The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.

While a View may need to display and present certain information it is not the View's job to go searching and retrieving information from the Model. The Controller can retrieve whatever information is relevant and pass that data along to the view for presentation.

There are a number of typical situations where it is necessary to pass data to a view:

In each of these situations the relevant action function will likely retrieve the information and pass it to the view as a view variable or variables. The following diagram represents the flow of a retrieve customer - edit customer - update customer flow.

The user supplies the id of the customer it wishes to modify. This generally happens when the user clicks on a URL of the form /shop/customer/edit/3. URLs of this form pass the last part of the url to the editAction($id) function of the CustomerController as a parameter. So in this example the user will modify the details of customer number 3.

At that point the editAction($id) function retrieves the details of customer number 3 and returns it as an object. The customer firstname and surname are retrieved and passed to the view as view variables using the syntax

as an object. The customer firstname and surname are retrieved and passed to the view as view variables using the syntax

$this->view->viewVariable = $someObject->getSomeAttribute()


Sometimes in my examples I will pass the entire object to the view and use the getters of the object within the view to retrieve data attributes there. This takes the form


This introduces a dependency on the model within the view. If you wanted to use that same view to present data from a different source this would be a problem. Strictly speaking when the view is accessing the data from the model directly we are coupling the view and the model and straying toward the ModelViewViewModel approach. Once you understand the rules and the reason they are there you can break them in full knowledge of the potential downsides. My justification would be that where a model class has numerous attributes it's simpler to set one view variable (the object) and then pull the attributes from the object as you need them. This is valid where passing additional un-necessary data to the view does not add a significant performance overhead and the view is unlikely to be reused. I'm open to discussion on this so feel free to comment below if you disagree with this.

****end aside****

To try out the example above save the following code to a file called /app/views/edit.phtml

<form action="<?php echo $this->url->getBaseUri()?>customer/update" method="post">
    <input type="hidden" name="id" value="<?php echo $id?>">
	Customer firstname: <input type="text" name="firstname" value="<?php echo $firstname?>">
    Cutomer Surname: <input type="text" name="surname" value="<?php echo $surname?>">
    <input type="submit">

Note the hidden field on the view which contains the customer id. This allows the updateAction to identify the customer being modified when the modified data is submitted. It's hidden because, the user will not and should not modify the unique identifier. The only other difference between this form and the new.phtml form is that the existing customer's firstname and surname are stored in the values of the fields.

Now update /app/controllers/CustomerController.php to introduce the editAction($id) and the updateAction() which display the view and process the results.

class CustomerController extends ControllerBase
    public function newAction()
    public function createAction()
        $customer = new Customer(); //creates a new instance of the customer object
        $customer->setFirstname($this->request->getPost('firstname'));//sets the attributes
        $customer->setSurname($this->request->getPost('surname'));//sets the attributes
        $customer->save();//persists the Customer object to the database
    public function editAction($id)
        $customer = Customer::findFirstById($id);
        $this->view->id = $customer->getid();
        $this->view->firstname = $customer->getFirstname();
        $this->view->surname = $customer->getSurname();

    public function updateAction()
        $customer = Customer::findFirstById($this->request->getPost('id'));