<span class=Warning
>
Status: Deprecated
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:
- Upgrade to Ubuntu 14.04.
- Upgrade from Ubuntu 14.04 to Ubuntu 16.04
- Migrate the server data to a supported version
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.
About PhalconPHP
Phalcon是一个http://phalconphp.com/en/框架,它促进了模型-视图-控制器体系结构,并具有许多与框架类似的功能,例如:ORM、模板引擎、路由、缓存等。 <br><br> 在本教程中,我们将从上次<a href=
https://www.digitalocean.com/community/articles/how-to-install-and-get-started-with-phalcon-on-an-ubuntu-12-04-vps`TARGET=`_BLACK`>停止的地方继续,当时我们在Ubuntu12.04VPS上安装了Phalcon,并使用Phalcon控制器将第一个字符串打印到屏幕上。在本教程中,我们将讨论其他两个核心MVC组件的使用:视图和模型。
为了继续阅读本文,我假设您已经完成了上一教程中概述的步骤,并且如果您将浏览器指向您的IP地址/项目名称,则让您的Phalcon应用程序打印出Hello World。所以,让我们开始行动吧。
Views
在上一教程中,我们使用一个方法(IndexAction)创建了默认的Index控制器,该方法执行以下操作:
echo "<h1>Hello World!</h1>";
As you know, this is not the best way to print things onto the screen; and like other PHP frameworks out there, Phalcon comes with a templating system that we should use instead. In other words, we can use a View and pass the Hello World string to it through a variable.
The way this works with Phalcon is that in the app/views folder we need to create another folder named as our controller and inside this folder a file named after the controller’s action. This way, Phalcon autoloads this view when the controller action is called. So let’s do this for our first controller/action Index. Create the folder first:
mkdir /var/www/project_name/app/views/index
Inside this folder, create a the file with a phtml extension (named after the controller action):
nano /var/www/project_name/app/views/index/index.phtml
Inside this file, cut the contents of the IndexAction from the controller and paste it in there, between tags:
<?php echo "<h1>Hello World!</h1>"; ?>
Now the controller action is empty, yet the string gets printed from the view that corresponds to that controller/action. So in this short example we have not passed anything to the view yet from the controller, we just demonstrated how the view is loaded automatically. Let’s now declare that variable in our controller, and then pass it to the view to be displayed.
Go back to the IndexController and inside the indexAction method, paste in the following (making sure this is all there is inside this function):
$string = "Hello World!"; $this->view->setVar("string", $string);
In the first line, we set our text value to the $string variable and in the second one we use the setVar method of the view() method of our parent controller class to send this variable to another one that can be accessed inside the view: also called $string.
Now edit the view and replace this:
<?php echo "<h1>Hello World!</h1>"; ?>
With this:
<h1><?php echo $string; ?></h1>
Now you should get the same thing in the browser. Like this, we separated logic (our string value that for all intents and purposes could have been dynamically generated or retrieved) from presentation (the html header tag we wrapped around the string in the View). This is one of the tenets of the MVC architecture and good modern programming practice.
Models and database
现在我们已经看到了控制器和视图,接下来让我们连接一个数据库,看看如何使用模型与其交互。接下来,假设您已经有了一个可以使用的数据库,您知道它的访问凭据,并且熟悉基本的MySQL命令。如果没有,请阅读<a href=https://www.digitalocean.com/community/articles/a-basic-mysql-tutorial
TARGET=_BLACK
>本教程。
要连接我们的数据库,我们需要编辑在上一篇文章中创建的引导文件,即位于public/文件夹中的index.php文件:
nano /var/www/project_name/public/index.php
Under this block:
//Setup a base URI so that all generated URIs include the "tutorial" folder $di->set( 39;url& 39;, function(){ $url = new \Phalcon\Mvc\Url(); $url->setBaseUri( 39;/project/& 39;); return $url; });
Add the following block:
//Setup the database service $di->set( 39;db& 39;, function(){ return new \Phalcon\Db\Adapter\Pdo\Mysql(array( "host" => "localhost", "username" => "root", "password" => "password", "dbname" => "db-name" )); });
And of course replace where appropriate with your database information. Save the file and exit. Next, create a table that will host your first model. Let’s call it articles and give it an ID column, a title column, and a body column. The following MySQL command will create it for you.
CREATE TABLE `articles` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `body` text NOT NULL, PRIMARY KEY (`id`) );
Let’s now insert a row through our command line to have something to play with. You can use this MySQL command to do it if you want:
INSERT INTO `articles` (title, body) VALUES ( 39;This is the first article& 39;, 39;some article body& 39;);
Now that we have some content, let's define our Article model. Create a php file in the app/models folder of your application:
nano /var/www/project_name/app/models/Articles.php
And paste this inside (omit the closing php tag):
<?php class Articles extends \Phalcon\Mvc\Model { }
We are now extending the Phalcon model class, which provides a lot of useful functionality for us to interact with our model data. Another cool thing is that since we named the class as we did the database table, the two are already linked. This model will refer to that database table.
Now what we need to do is declare our model properties (that map to the table columns). So inside the class, add the following protected properties:
public $id; public $title; public $body;
Next, we need some setters/getters to retrieve from or to assign values to these protected properties. And here we can have a lot to control over how the data can be accessed. But since this tutorial will not look into adding information to the database, we will only add one getter function to retrieve existing information from the property. So below, but still inside the model class, add the following methods:
public function getId() { return $this->id; } public function getTitle() { return $this->title; } public function getBody() { return $this->body; }
Normally however, you’ll also need setter functions. Let's save this file and turn back to our IndexController. Inside the IndexAction, replace all the contents with the following:
$article = Articles::findFirst(1); $this->view->setVar("string", $article->getTitle());
On the first line we use the findFirst method of the Phalcon model class from which we extended our Articles model to retrieve the article with the ID of 1. Then in the second one, we pass to the View the value of the title column that we are retrieving with our getter function we declared in the model earlier. Save the file and reload the browser. You should see printed out the title of the first article.
Since we cannot go into all what you can do with the Phalcon model class, I encourage you to read more about it here. You will find a bunch of ready functionality and database abstraction to get you going fast.
Conclusion
在本教程中,我们了解了Phalcon如何在给定视图在项目文件夹结构中的位置的情况下自动加载视图,以及如何将信息从控制器操作传递到其各自的视图。此外,我们还建立了一个小型Phalcon模型,以了解如何与数据库表交互并检索信息。