Phalcon入门,一个PHP框架-第2部分
状态: 已弃用
本文介绍了不再受支持的 Ubuntu 版本。 如果您当前正在运行运行 Ubuntu 12.04 的服务器,我们强烈建议您升级或迁移到受支持的 Ubuntu 版本:
原因: Ubuntu 12.04 已于 2017 年 4 月 28 日终止生命周期 (EOL) and no longer receives security patches or updates. This guide is no longer maintained.
请参阅:
本指南可能仍可用作参考,但可能不适用于其他 Ubuntu 版本。 如果可用,我们强烈建议使用为您正在使用的 Ubuntu 版本编写的指南。 您可以使用页面顶部的搜索功能来查找更新的版本。
关于 PhalconPHP
Phalcon 是一个 PHP 框架,它促进了模型-视图-控制器架构,并具有许多类似框架的功能,您可以在这样的软件中使用:ORM、模板引擎、路由、缓存等.
在本教程中,当我们在 Ubuntu 12.04 VPS 上安装 Phalcon 并设法将第一个字符串打印到使用 Phalcon 控制器的屏幕。 在本教程中,我们将涉及使用其他两个核心 MVC 组件:视图和模型。
要继续阅读本文,我假设您已经完成了上一教程中概述的步骤,并且如果您指出您的 Phalcon 应用程序会打印出 Hello World浏览器到您的 ip 地址/项目名称 。 所以让我们深入研究。
意见
在上一教程中,我们使用一种方法 (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 你好世界 string to it through a variable.
The way this works with Phalcon is that in the 应用程序/视图 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 指数. 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 索引控制器 and inside the index动作 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 $字符串 variable and in the second one we use the 设置变量 method of the 看法() method of our parent controller class to send this variable to another one that can be accessed inside the view: also called $字符串.
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.
模型和数据库
现在我们已经看到了控制器和视图,让我们连接一个数据库,看看我们如何使用模型与它进行交互。 从这里继续假设您已经有一个可以使用的数据库,您知道它的访问凭据,并且熟悉基本的 MySQL 命令。 如果没有,请查看本教程。
要连接我们的数据库,我们需要编辑我们在上一篇文章中创建的引导文件,位于 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('url', function(){ $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/project/'); return $url; });
Add the following block:
//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" )); });
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 文章 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 ('This is the first article', 'some article body');
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 索引控制器. 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 查找第一 method of the Phalcon model class from which we extended our 文章 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 这里. You will find a bunch of ready functionality and database abstraction to get you going fast.
结论
在本教程中,我们看到了 Phalcon 如何根据项目文件夹结构中的位置自动加载视图,以及如何将信息从控制器操作传递到其各自的视图。 此外,我们建立了一个小型 Phalcon 模型,以了解我们如何与数据库表交互并检索信息。