Phalcon 1.1 beta
After the successful release of the PHP framework Phalcon 1.0 , the development team continues to work on its development. In this article I want to highlight the most interesting features presented in version 1.1.0 of BETA
- Pagination of query designer results (QueryBuilder)
- Beanstalkd Queue Server
- Encryption
- Assets management
- Exception mode for ORM validators
- Host Name Routing
- Using controllers in Mvc \ Micro applications
- Afterword
- Help the community by voting for Phalcon support at cPanel
Pagination in the query designer (QueryBuilder)
Previously, the paginator was available only for outputting Model data and native arrays. Pagination can now be used to output arbitrary query data through the QueryBuilder constructor, which uses SQL LIMIT / OFFSET statements. It will be useful for outputting large data sets.
use Phalcon\Paginator\Adapter\QueryBuilder;
$builder = $this->modelsManager->createBuilder()
->columns('id, name')
->from('Robots')
->orderBy('name');
$paginator = new Paginator(array(
"builder" => $builder,
"limit" => 10,
"page" => 1
));
$page = $paginator->getPaginate();
Beanstalkd Queue Server
A simple client for the Beanstalkd Queue Server is now part of the framework.
// Соединение с сервером
$queue = new Phalcon\Queue\Beanstalk(array(
'host' => '192.168.0.21'
));
// Добавить задачу в очередь (простая запись)
$queue->put(array('proccessVideo' => 4871));
// Добавить задачу в очередь (с параметрами)
$queue->put(
array('proccessVideo' => 4871),
array('priority' => 250, 'delay' => 10, 'ttr' => 3600)
);
while (($job = $queue->peekReady()) !== false)
{
$message = $job->getBody();
var_dump($message);
$job->delete();
}
Encryption
Added encryption class based on mcrypt PHP library to this version of Phalcon
// Создаём экземпляр класса шифрования
$encryption = new Phalcon\Crypt();
$key = 'le password';
$text = 'This is a secret text';
$encrypted = $encryption->encrypt($text, $key);
echo $encryption->decrypt($encrypted, $key);
Assets management
Using this component, you can easily manage static resources such as CSS and Javascript.
// Сначала в контроллере добавим немного CSS
$this->assets
->addCss('css/style.css')
->addCss('css/index.css');
// и чуть-чуть js-скриптов
$this->assets
->addJs('js/jquery.js')
->addJs('js/bootstrap.min.js');
and then show them in the template
Some amazing website
assets->outputCss() ?>
assets->outputJs() ?>
Exception mode for ORM validators
During data validation, in the process of creating / updating a database record, the save () / create () / update () methods return a Boolean value, i.e. FALSE if one of the parameters did not pass validation. Now you can change this behavior and throw an exception:
use Phalcon\Mvc\Model\ValidationFailed;
try {
$robot = new Robots();
$robot->name = 'Bender';
$robot->save();
} catch (ValidationFailed $e) {
echo 'Reason: ', $e->getMessage();
}
Host Name Routing
In the routing rules, you can now specify the host name
$router = new Phalcon\Mvc\Router();
$router->addGet('/api/robots', array(
'module' => 'api',
'controller' => 'robots',
'action' => 'index'
))->setHostName('api.phalconphp.com');
You can also use the route group.
$group = new Phalcon\Mvc\Router();
$group->setHostName('api.phalconphp.com');
$groop->addGet('/api/robots', array(
'module' => 'api',
'controller' => 'robots',
'action' => 'index'
));
$groop->addGet('/api/robots/{id}', array(
'module' => 'api',
'controller' => 'robots',
'action' => 'show'
));
$router->mount($group);
Using controllers in Mvc \ Micro applications
To better organize the structure of the micro-application in the new version of Phalcon, controllers can be specified as a request handler (previously only callable values could be used).
$collection = new Phalcon\Mvc\Micro\Collection();
// Немедленная инициализация
$collection
->setPrefix('/posts')
->setHandler(new PostsController());
// Ленивая инициализация
$collection
->setPrefix('/posts')
->setHandler('PostsController', true);
$collection->get('/', 'index');
$collection->get('/edit/{id}', 'edit');
$collection->delete('/delete/{id}', 'delete');
$app->mount($collection);
Afterword
Phalcon 1.1.0 includes other changes and bug fixes. The full list of changes can be found in CHANGELOG , as well as read the documentation for this version of the framework.
If you still have not personally tested the performance of Phalcon, you can install it right now
git clone http://github.com/phalcon/cphalcon
cd build
git checkout 1.1.0
sudo ./install
Windows users only need to install the DLL from the download page .
Developers are invited to discuss this release on the forum (which, incidentally, is also written in Phalcon) and Stack Overflow .
If you catch a bug, Github will be happy to accept a pull request or failing test.
Help the community by voting for Phalcon support at cPanel
Phalcon developers offer to add support for the framework to the cPanel web hosting control panel .
If Phalcon becomes an affordable extension for cPanel clients, this will not only increase the popularity of the framework, but also benefit developers, hosting companies, and subsequently end users, because Phalcon is written in C, which means that it consumes less memory and creates less load on server in comparison with analogues written in PHP.
You can support the development of the framework by voting for the feature request on the cPanel website:
http://features.cpanel.net/responses/add-support-for-phalconphp-extension-apache-php
Sources:
PS The text has been translated and prepared by agent_j , which, for certain reasons, cannot place it on its own.