Zend Framework 2 — the long awaited improvements in Controller and View
recently tried a new ZF2. Read a bunch of material gathered in their tutorial simple site. And when it came down to creating Action and View'hee that limit my joy knew no bounds. Passing variables in the view now is through a return, and they were local (no $this->param). Just for the sake of improvement I am ready now to move on to ZF2, despite the fact that it is in beta.
Under the cut you will: brief about innovations, about performance and about the changes regarding controller and view function templates.
Finally, it is finished! In action controllers pass a variable to have ceased to be magic. Instead of the ugly $this->view->... now action must return an associative array with the data for the view function. Give examples for comparison.
it Was ZF1:
Became ZF2:
View templates has also changed for the better. Local variables (accessible by their name), and helpers remain available through $this->helper(). All string variables automatically escapade, in order to avoid XSS. Arrays and objects remain unchanged, so the output of these values, you must pass through $this->escape();
Writing and reading code easier.
it Was ZF1:
Became ZF2:
In my opinion this is the most "delicious" improvement in ZF2.
Zend Framework 2 is in beta. Somewhere I saw that the stable release will be in spring 2012. But use at your own risk you can now.
Be sure to place the latest PHP 5.3.8. With PHP 5.3.2 I have the error Strict standard.
Under Windows I had to put a new Apache 2.4 compiled under VC9. To download PHP x86 TS VC9 here, extensions apc and mongo here, php_xdebug.dll to find the official website sending in mold your phpinfo.
First read a good article in Russian "the Zend Framework 2. Materials for the study" tokarchuk.ru.
Then take the wonderful tutorial Getting Started with Zend Framework 2 (PDF, en), with which you for an hour or two install ZF2 and create test module "album".
Article based on information from habrahabr.ru
Under the cut you will: brief about innovations, about performance and about the changes regarding controller and view function templates.
Innovation ZF2, which is spoken by all:
-
the
- improved performance the
- use more flexible approaches, improvements in standardization and unification the
- redesigned model working with plug-ins the
- widespread use namespace's the
- no more require_once calls inside the component (all using the autoloader) the
- 2 new fundamental component Dependency Injection and EventManager (DI the thing is undeniably cool, but scares his magic settings in the object name of the variables in the methods. Usually, this magic has a negative impact on performance. EventManager handy thing. Logging, caching, pre - and post-data processing is now at the highest level)
About ZF2 performance
-
the
- due to new autoloader Class Map gives up to 85% performance improvement compared to ZF1 the
- using namespace's up to 40% (first thought it was a minor bun in PHP5.3 and is purely aesthetic in nature) the
- reduced number of calls when using plug-ins (7 calls instead of 13, I think we can talk about a two-fold acceleration) the
- refactoring of the MVC (must give a tangible performance boost. Has improved usability.)
the long-Awaited improvements in Controller and View
Finally, it is finished! In action controllers pass a variable to have ceased to be magic. Instead of the ugly $this->view->... now action must return an associative array with the data for the view function. Give examples for comparison.
it Was ZF1:
public function indexAction()
{
$this->view->albums = $this->albumTable->fetchAll();
$this->view->user = 123;
$this->view->text = 'any <html> it will not be automatically escaped in view';
}
Became ZF2:
public function indexAction()
{
return array(
'albums' => $this->albumTable->fetchAll(),
'user' => 123,
);
}
View templates has also changed for the better. Local variables (accessible by their name), and helpers remain available through $this->helper(). All string variables automatically escapade, in order to avoid XSS. Arrays and objects remain unchanged, so the output of these values, you must pass through $this->escape();
Writing and reading code easier.
it Was ZF1:
<?php
$this->headTitle('My albums');
?>
<?php
foreach($this->albums as $album) {
// some code
}
?>
<?php
echo $this->escape($this->text);
?>
Became ZF2:
<?php
// helpers are still available via $this
$this->headTitle('My albums');
?>
<?php
// albums of the action and, in the example above became available via a local variable
foreach($albums as $album) {
// some code
}
?>
<?php
// no need to call escape, all scalar values in the machine escapade
// arrays and objects in the view are ignored and fall as it is
echo $text;
?>
In my opinion this is the most "delicious" improvement in ZF2.
How to try and something to read
Zend Framework 2 is in beta. Somewhere I saw that the stable release will be in spring 2012. But use at your own risk you can now.
Be sure to place the latest PHP 5.3.8. With PHP 5.3.2 I have the error Strict standard.
Under Windows I had to put a new Apache 2.4 compiled under VC9. To download PHP x86 TS VC9 here, extensions apc and mongo here, php_xdebug.dll to find the official website sending in mold your phpinfo.
First read a good article in Russian "the Zend Framework 2. Materials for the study" tokarchuk.ru.
Then take the wonderful tutorial Getting Started with Zend Framework 2 (PDF, en), with which you for an hour or two install ZF2 and create test module "album".
Комментарии
Отправить комментарий