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? (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 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]

The Phlacon documentation suggests using the following directory structure:



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.


try {
// register an autoloader
$loader = new PhalconLoader();

// create a dependency injection container
$di = new PhalconDIFactoryDefault();

//set up the view component
$di->set('view', function(){
$view = new PhalconMvcView();
return $view;

// handle the request
$application = new PhalconMvcApplication();
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。

<title>Blog's title</title>
<?php echo $this->getContent(); ?>

Phalcon does however has a flash messaging system built-in:


$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:


$query = new PhalconMvcModelQuery("SELECT * FROM Users", $di);
$users = $query->execute();

And instead of such raw SQL, you can use the query builder like this:


$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



  • 0
  • 0
  • 0
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
Phalcon一个开放源码的、全堆栈的PHP5框架,使用C扩展编写,专门为高性能优化,无需使用C语言,所有函数都以PHP类的方式。PhalconPHP 3.4.0 更新日志:2018-05-28添加Phalcon\Mvc\Router::attach直接将路由对象添加到路由器#13326中;增加了侦听请求的功能:beforeAuthorizationResolve和request:afterAuthorizationResolve事件,这种能力可以使用自定义授权解析器#13327;在Phalcon\Mvc\Model中添加呼叫事件afterFetch:刷新#12220;添加Phalcon\Http\Response::getReasonPhrase以从状态报头#13314中检索原因词组;添加Phalcon\Loader::setFileCheckingCallback来设置内部文件存在解析器#13360;增加了为Phalcon\Mvc\Collection::aggregate#12302传递聚合选项的功能;添加Phalcon\Crypt::setHashAlgo来设置用于计算消息摘要的散列算法的名称#13379;添加Phalcon\Crypt::getHashAlgo以获得用于计算消息摘要的散列算法的名称#13379;添加Phalcon\Crypt::useSigning来设置是否必须使用计算消息摘要(注意:此功能将在Phalcon4.0.0中默认启用)#13379;添加了Phalcon\Crypt::getAvailableHashAlgos以获得适用于计算消息摘要的注册散列算法列表#13379;添加了Phalcon\Crypt::__构造,现在可以在对象构造中设置密码,并且可以启用计算消息摘要,而无需明确调用setCipher或useSigning#13379;添加了Phalcon\Crypt\Mismatch,在Phalcon\Crypt中抛出的异常将使用这个类#13379;添加Phalcon\Http\Cookie::setSignKey来设置用于生成消息认证代码的符号密钥(例如消息摘要);添加了Phalcon\Http\Response\Cookies::setSignKey来设置用于生成消息认证代码的符号密钥(例如消息摘要);更改了Phalcon\Crypt::setCipher,以便在设置密码算法期间重新配置IV长度;更改了Phalcon\Crypt::setCipher,以便在密码不可用的情况下抛出Phalcon\Crypt\Exception;修复Phalcon\Debug\Dump::output的回归(#13308)以正确使用详细模式#13315;修正Phalcon\Mvc\Model\Query\Builder::having和Phalcon\Mvc\Model\Query\Builder::在哪里正确合并绑定类型#11487;修正Phalcon\Mvc\Model::setSnapshotData以正确设置旧快照;超级全球不存在时不要抛出异常#13252,#13254,#12918。
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
钱包余额 0