This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.
Phalcon is a PHP framework that promotes the Model-View-Controller architecture and has many framework-like features you’d expect in a piece of software such as this: ORM, templating engine, routing, caching, etc.
In this tutorial, we will continue where we left off the last time when we got Phalcon installed on our Ubuntu 12.04 VPS and managed to print our first string to the screen using a Phalcon controller. In this tutorial, we will touch upon using the other two core MVC components: the views and the models.
To follow along this article, I assume you have gone through the steps outlined in the previous tutorial and have your Phalcon application printing out Hello World if you point your browser to your ip-address/project-name. So let’s dig in.
In the previous tutorial we created the default Index controller with one method (IndexAction) that does this:
echo "<h1>Hello World!</h1>";
mkdir /var/www/project_name/app/views/index
nano /var/www/project_name/app/views/index/index.phtml
<?php echo "<h1>Hello World!</h1>"; ?>
$string = "Hello World!"; $this->view->setVar("string", $string);
<?php echo "<h1>Hello World!</h1>"; ?>
<h1><?php echo $string; ?></h1>
Now that we’ve seen controllers and views, let’s connect a database and see how we can interact with it using models. Continuing from here assumes you have already a database you can use, you know its access credentials, and are familiar with basic MySQL commands. If not, check out this tutorial.
To connect our database, we need to edit the bootstrap file we created in the previous article, the index.php file located in the public/ folder:
nano /var/www/project_name/public/index.php
//Setup a base URI so that all generated URIs include the "tutorial" folder $di->set('url', function(){ $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/project/'); return $url; });
//Setup the database service $di->set('db', function(){ return new \Phalcon\Db\Adapter\Pdo\Mysql(array( "host" => "localhost", "username" => "root", "password" => "password", "dbname" => "db-name" )); });
CREATE TABLE `articles` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `body` text NOT NULL, PRIMARY KEY (`id`) );
INSERT INTO `articles` (title, body) VALUES ('This is the first article', 'some article body');
nano /var/www/project_name/app/models/Articles.php
<?php class Articles extends \Phalcon\Mvc\Model { }
public $id; public $title; public $body;
public function getId() { return $this->id; } public function getTitle() { return $this->title; } public function getBody() { return $this->body; }
$article = Articles::findFirst(1); $this->view->setVar("string", $article->getTitle());
In this tutorial, we’ve seen how views are automatically loaded by Phalcon given their placement in the project folder structure and also how to pass information from the controller action to its respective view. Additionally, we’ve set up a small Phalcon model to see how we can interact with a database table and retrieve information.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.