⬆️ ⬇️

Zend Framework 2 - long-awaited improvements in Controller and View

image The other day I tried the new ZF2. I re-read a bunch of material, collected a simple saytik on their tutorial. And when I reached the creation of Action and View'hi, then the limit of my joy knew no bounds. Passing variables to the view is now done via return, and they become local (no $ this-> param). Only for the sake of this improvement, I am ready to switch to ZF2 right now, despite the fact that it is in beta.



Under the cat you will find: a brief about innovations, about performance and about changes regarding the controller and view templates.





Innovations ZF2, which everyone talks about:

The developers tried and did a decent ZendSkeletonApplication - this is a simple application skeleton, using the ZF2 MVC layer and system modules. The skeleton uses free css and html tools from twitter. We get a nice web2.0 site from one page that is easy to inflate to a full-fledged site.

')

Concerning ZF2 performance

It is difficult to estimate, but it is safe to assume that productivity will increase no less than twice.



Long-awaited improvements in Controller and View



Finally accomplished! In action controllers, the transfer of variables has ceased to be magic. Instead of the ugly $ this-> view -> ..., now the action should return an associative array with the data for the view. I will 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' ;

}


It became ZF2:

public function indexAction ( )

{

return array (

'albums' => $ this -> albumTable -> fetchAll ( ) ,

'user' => 123 ,

'text' => 'any <html> it will be automatically escaped in view' ,

) ;

}




View templates are also transformed for the better. Variables became local (available by their name), and helpers remained available through $ this-> helper (). All string variables are automatically escaped to avoid XSS. Arrays and objects remain unchanged, therefore, when deriving values ​​from them, it is necessary to pass through $ this-> escape ();

Writing and reading code has become more convenient.



It was ZF1:

<? php

$ this -> headTitle ( 'My albums' ) ;

?>



<? php

foreach ( $ this -> albums as $ album ) {

// some code

}

?>



<? php

echo $ this -> escape ( $ this -> text ) ;

?>



It became ZF2:

<? php

// all helpers are also available via $ this

$ this -> headTitle ( 'My albums' ) ;

?>



<? php

// the albums from the action from the example above are available via a local variable

foreach ( $ albums as $ album ) {

// some code

}

?>



<? php

// there is no need to call escape, all scalar values ​​are automatically escaped

// arrays and objects are skipped and get into view as is

echo $ text ;

?>



In my opinion this is the most "delicious" improvement in ZF2.



How to try and what to read



Zend Framework 2 is in beta state. I saw somewhere that the stable release will be in the spring of 2012. But you can use it at your own risk now.



Be sure to put the latest PHP 5.3.8. On PHP 5.3.2, I got a Strict Standart error.

Under Windows, I had to install a new Apache 2.4 compiled for VC9 . Download PHP x86 TS VC9 from here , extensions apc and mongo from here , pick up php_xdebug.dll on the official website by sending your phpinfo in a form.



First of all, read a good article in Russian "Zend Framework 2. Materials for study" on the site tokarchuk.ru.



Then use the wonderful tutorial Getting Started with Zend Framework 2 (PDF, en), with which you will install ZF2 in an hour or two and create a test module "album".

Source: https://habr.com/ru/post/135768/



All Articles