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.

There are a number of standard diagramatic approaches used to represent MVC diagrams. In the most common of these the Models, Views and Controllers are represented as three boxes with the User interacting with the Controller and being presented with information from the view. While this can be a useful way to look at the different components I prefer to use a UML sequence diagram to understand the flow of steps involved. Consider the following simplified example which describes a user filling in a html form which will allow for the creation of a new Customer in the system.


The user will interact with the system to indicate the desire to create a new Customer i.e. they will call the newAction of the CustomerController class. The Customer controller will call the view to generate the necessary html. The user will complete the form and submit which will call the createAction of the CustomerController. This then instantiates a new Customer Model object, sets the necessary attributes and saves the data.

<form action="create" method="post">
    Customer firstname: <input type="text" name="firstname"><br>
    Cutomer Surname: <input type="text" name="surname"><br>
    <input type="submit">
</form>

In Phalcon a form such as this is a "view" designed to capture basic information about a customer's name. The code above will work fine in Phalcon if it has a Controller/Action associated with it and if it is placed in the correct folder/filename which corresponds to that Controller/Action i.e. The folder must have the same name as the controller and the view filename must be the same as the action name (with the word "Action" omitted). This corresponding naming approach is a good use of the "Convention over Configuration" principle.

This controller newAction function can be completely empty if there is nothing to be done before rendering the view -in which case the Controller and Action will look like this.

<?php
class CustomerController extends ControllerBase
{
    public function newAction()
    {
        
    }
}
?>

Phalcon views which have been generated by the scaffolder do not look like the HTML Form above. Views which have been generated by the scaffolder will use "tags" to create input controls and labels on the form. Tags can be useful but they are an additional syntax and approach to learn. I tend to avoid them unless they offer me a simple direct benefit. Ultimately the tags are converted to a combination of HTML and PHP in order to be rendered in the browser.

Fill in a firstname and surname for the customer and click submit - you get the following error:

To try this out create a simple phalcon project called shop - from the CLI type

phalcon project shop

Copy the html code at the top of the page above into a file called /shop/app/views/customer/new.phtml. (you'll have to create the customer folder under the views folder - we're not scaffolding this one).

Save the controller code above to a file called /app/controllers/CustomerController.php

Try this out by visiting http://localhost/shop/customer/new - Within this url the customer/new calls the Customer controller and the newAction() function of that controller. In most MVC-based applications the url breaks down as follows http://hostname/appName/controller/functionOfTheController. When the URL Request is executed within the MVC application the newAction() function of the CustomerController is called - the new.phtml view is rendered - the user sees the form within that view.

Fill in the form with some test data and click submit - you'll get an error like this.


The action on the form is "create" so when the form is submitted phalcon will attempt to call the createAction() function of the CustomerController. This doesn't exist yet so you get the error.

Modify the CustomerController.php file to include a createAction() function - the Controller should now appear as follows:

<?php
class CustomerController extends ControllerBase
{
    public function newAction()
    {
        
    }
    
    public function createAction()
    {
        echo "Firstname=" . $this->request->getPost('firstname') . "<br>";
        echo "Surname=" . $this->request->getPost('surname');
    }
}
?>

Now try the form again by visiting http://localhost/shop/customer/new In our example that means the firstname and surname should be printed to the screen. In the next post we'll introduce the Model into our simple example.