There’s a wide offering of PHP frameworks, from full-stack frameworks containing ORMs, validation components, and loads of HTML helpers, to micro frameworks which go little beyond offering routing functionality. They all claim to be special, either with beautiful syntax, high speed, or good documentation. One of those frameworks is Phalcon. But Phalcon really is quite different compared to the other frameworks; it isn’t just another package that you download, rather it’s a PHP module written in C.
PHP框架的范围很广,从包含ORM的全栈框架,验证组件和HTML帮助程序的负载到微型框架,这些框架仅提供路由功能。 他们都声称自己很特别,语法优美,速度快或文档完善。 这些框架之一就是Phalcon。 但是Phalcon与其他框架相比确实有很大的不同。 它不仅仅是您下载的另一个软件包,而是一个用C编写PHP模块。
In this article we’ll take a brief look at what Phalcon looks like and what makes it so special.
在本文中,我们将简要介绍Phalcon的外观及其特殊之处。
什么是Phalcon? (What is Phalcon?)
Phalcon is a full-stack framework. It promotes the MVC architecture and offers features like an ORM, a Request object library, a templating engine, caching, pagination… a full list of features can be found on it’s website.
Phalcon是一个全栈框架。 它促进了MVC架构,并提供了ORM,请求对象库,模板引擎,缓存,分页等功能,可以在其网站上找到完整的功能列表。
But Phalcon is somewhat unique because you don’t just download an archive and extract it to a directory like you do with most other frameworks. Instead, you download and install Phalcon as a PHP module. The install process doesn’t take much more than a few minutes, and installation instructions can be found in the documentation. Also, Phalcon is open-source. You can always modify the code and recompile it if you want.
但是Phalcon有点独特,因为您不像大多数其他框架一样下载存档并将其解压缩到目录中。 而是将Phalcon下载并安装为PHP模块。 安装过程不会超过几分钟,并且安装说明可以在文档中找到。 而且,Phalcon是开源的。 您始终可以修改代码并根据需要重新编译。
编译以获得更好的性能 (Compiled for Better Performance)
One major drawback for PHP is that on every request, all files are read from the hard drive, translated into bytecode, and then executed. This causes some major performance loss when compared to other languages like Ruby (Rails) or Python (Django, Flask). With Phalcon the whole framework already is in RAM, so the whole set of framework files don’t need to process. There are benchmarks on the website that show indeed this has some significant performance advantages.
PHP的一个主要缺点是,在每次请求时,都会从硬盘驱动器读取所有文件,将其转换为字节码,然后执行。 与Ruby(Rails)或Python(Django,Flask)之类的其他语言相比,这会导致一些重大的性能损失。 使用Phalcon,整个框架已经在RAM中,因此不需要处理整个框架文件。 网站上有一些基准测试表明,这确实具有明显的性能优势。
![phalcon-01](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2013/07/phalcon-01.png)
Phalcon serves more than double of CodeIgniter’s requests per second. And when you look at the time per request, Phalcon takes the least amount of time to handle requests. So whenever a framework says that it’s fast, think that Phalcon is even faster.
Phalcon每秒可处理CodeIgniter请求的两倍以上。 而且,当您查看每个请求的时间时,Phalcon花费的时间最少。 因此,只要框架说它很快,就认为Phalcon甚至更快。
使用Phalcon (Using Phalcon)
Phalcon offers the classic features of a modern PHP MVC-framework (routing, controllers, view template, ORM, Caching, etc.), so there is nothing special when compared to other frameworks except for its speed. Still, let’s take a look at what a typical project using Phalcon looks like. First, there’s usually a bootstrap file which will be called on every request. The requests are sent to the bootstrap via directives stored in an .htaccess
file.
Phalcon提供了现代PHP MVC框架的经典功能(路由,控制器,视图模板,ORM,缓存等),因此与其他框架相比,除了速度方面没有什么特别之处。 尽管如此,让我们看一下使用Phalcon的典型项目的外观。 首先,通常会有一个引导文件,每个请求都会被调用。 通过存储在.htaccess
文件中的指令将请求发送到引导程序。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
The Phlacon documentation suggests using the following directory structure:
Phlacon文档建议使用以下目录结构:
app/
controllers/
models/
views/
public/
css/
img/
js/
But the directory layout can be modified if you really want since everything will be accessed via the bootstrap file which exists as public/index.php
.
但是,如果您确实需要,可以修改目录布局,因为所有内容都可以通过存在于public/index.php
的引导文件进行访问。
<?php
try {
// register an autoloader
$loader = new PhalconLoader();
$loader->registerDirs(array(
'../app/controllers/',
'../app/models/'
))->register();
// create a dependency injection container
$di = new PhalconDIFactoryDefault();
//set up the view component
$di->set('view', function(){
$view = new PhalconMvcView();
$view->setViewsDir('../app/views/');
return $view;
});
// handle the request
$application = new PhalconMvcApplication();
$application->setDI($di);
echo $application->handle()->getContent();
}
catch (PhalconException $e) {
echo "PhalconException: ", $e->getMessage();
}
模型控制器 (Model-Controller)
The controllers and models are autoloaded, so you can just create files and use them from anywhere in the project. Controllers should extend PhalconMvcController
and models extend PhalconMvcModel
.
控制器和模型是自动加载的,因此您可以创建文件并在项目中的任何位置使用它们。 控制器应扩展PhalconMvcController
,模型应扩展PhalconMvcModel
。
Controller actions are defined like so:
控制器动作的定义如下:
public function indexAction() {
echo 'welcome to index';
}
Models too are pretty straight-forward:
模型也很简单:
class Users extends PhalconMvcModel
{
}
By extending the PhalconMvcModel
class you immediately have access to some handy methods, like find()
, save()
, and validate()
. And you can use relationships like:
通过扩展PhalconMvcModel
类,您可以立即访问一些方便的方法,例如find()
, save()
和validate()
。 您可以使用如下关系:
class Users extends PhalconMvcModel
{
public function initialize() {
$this->hasMany('id', 'comments', 'comments_id');
}
}
观看次数 (Views)
Views offer basic functionality like being able to pass data to your views and working with layouts. Phalcon views doesn’t use special syntax though like Twig or Blade, though. They use pure PHP.
视图提供了基本功能,例如能够将数据传递到视图和使用布局。 尽管Phalcon视图不像Twig或Blade那样使用特殊语法。 他们使用纯PHP。
<html>
<head>
<title>Blog's title</title>
</head>
<body>
<?php echo $this->getContent(); ?>
</body>
</html>
Phalcon does however has a flash messaging system built-in:
但是,Phalcon确实具有内置的Flash消息传递系统:
$this->flashSession->success('Succesfully logged in!');
Phalcon查询语言 (Phalcon Query Language)
Phalcon has its own ORM, Phalcon Query Language (PHQL), which can be used to make database interaction more expressive and clean. PHQL can be integrated with models to easily define and use relationships between your tables.
Phalcon拥有自己的ORM,即Phalcon查询语言(PHQL),可用于使数据库交互更富有表现力和简洁。 PHQL可以与模型集成,以轻松定义和使用表之间的关系。
You can use PHQL by extending the PhalconMvcModelQuery
class and then create a new query like:
您可以通过扩展PhalconMvcModelQuery
类来使用PHQL,然后创建一个新查询,例如:
$query = new PhalconMvcModelQuery("SELECT * FROM Users", $di);
$users = $query->execute();
And instead of such raw SQL, you can use the query builder like this:
而且,除了使用这种原始SQL之外,您还可以使用如下查询构建器:
$users = $this->modelsManager->createBuilder()->from('Users')->orderBy('username')->getQuery()->execute();
This can come in quite handy when your queries get more complicated.
当查询变得更加复杂时,这会非常方便。
结论 (Conclusion)
Phalcon offers the classic features of a modern PHP MVC-framework so it should be comfortable to use, so in that sense it is just another PHP framework. But where it really stands out from the others is its speed. If you’re are interested in learning more about Phalcon, check out the framework’s documentation. Make sure you try it out!
Phalcon提供了现代PHP MVC框架的经典功能,因此使用起来应该很舒适,因此从某种意义上讲,它只是另一个PHP框架。 但是它真正与众不同的地方在于它的速度。 如果您有兴趣了解有关Phalcon的更多信息,请查看框架文档 。 确保尝试一下!
Image via Fotolia
图片来自Fotolia
翻译自: https://www.sitepoint.com/phalconphp-yet-another-php-framework/