📜 ⬆️ ⬇️

Release Phalcon 2.1.0 beta 1

We are pleased to present you the first beta release of Phalcon 2.1 !

2.1.x releases will be supported for a longer period, 2.1 will be our first version with long-term support ( LTS ).

In 2.0.x we introduced several new features and fixed a lot of bugs. However, our attention has always been paid to maintaining backward compatibility with Phalcon 1.3.x, while at the same time we encouraged developers to upgrade to 2.0.x. This gave enough time for developers to make changes to their applications to work with the new branch.
')
Phalcon 2.1 provides new features, some of which are incompatible with previous versions, so make sure you check your applications before updating production systems.

We are sure that the changes in this release will justify the update :)

End of PHP 5.3 support


Phalcon 2.0.x - the latest release series with PHP 5.3 support (> = 5.3.21). Because of this limitation, we cannot incorporate some performance improvements into the framework.

Since version 2.1.x, we strongly recommend recommending developers to upgrade to 5.6, and PHP7 is already there. We are focused on working with PHP 7, but at the same time the recommended PHP version for working now is 5.6.

Phalcon\Mvc\Model\Validation been deprecated


Phalcon\Mvc\Model\Validation replaced by Phalcon\Validation . The functionality of both components merges into one, thereby simplifying the support of the code base.

Previously, validation was carried out as follows:

 namespace Invo\Models; use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Validator\Email as EmailValidator; use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator; class Users extends Model { public function validation() { $this->validate( new EmailValidator( [ 'field' => 'email', ] ) ); $this->validate( new UniquenessValidator( [ 'field' => 'username', 'message' => 'Sorry, That username is already taken', ] ) ); if ($this->validationHasFailed() == true) { return false; } } } 

With the introduction of Phalcon\Validation , you will need to change the above code:

 namespace Invo\Models; use Phalcon\Mvc\Model; use Phalcon\Validation; use Phalcon\Validation\Validator\Email as EmailValidator; use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator; class Users extends Model { public function validation() { $validator = new Validation(); $validator->add( 'email', new EmailValidator() ); $validator->add( 'username', new UniquenessValidator( [ 'message' => 'Sorry, That username is already taken', ] ) ); return $this->validate(); } } 

Agree, this change makes the code much more readable.

Changes in the designer Phalcon\Mvc\Model


The class model constructor has been modified to allow you to pass an array of data for initialization:

 $customer = new Customer( [ 'name' => 'Peter', 'status' => 'Active', ] ); 

Using this method is analogous to assign() , i.e. an available setter (implemented in a specific model or embedded) will be used to assign a property.

Phalcon\Mvc\View supports multiple view directories.


It was one of the features that our community has requested many times. We are glad to announce that now you can use any kind of hierarchy when specifying directories with views. This is especially useful for reuse in multiple modules:

 use Phalcon\Mvc\View; // ... $di->set( 'view', function () { $view = new View(); $view->setViewsDir( [ '/var/www/htdocs/blog/modules/backend/views/', '/var/www/htdocs/blog/common/views/', ] ); return $view; } ); 

Phalcon\Mvc\View now supports absolute paths.


The absolute path can now be used in Mvc\View::setLayoutsDir and Mvc\View::setPartialsDir . This allows you to use folders outside the main view directory.

 use Phalcon\Mvc\View; // ... $di->set( 'view', function () { $view = new View(); $view->setViewsDir( [ '/var/www/htdocs/blog/modules/backend/views/', '/var/www/htdocs/blog/common/views/', ] ); $view->setLayoutsDir( '/var/www/htdocs/common/views/layouts/' ); return $view; } ); 

Phalcon\Di now tied to service closures


In the past, we had to pass the dependency container inside the service closure if it was necessary to perform some actions inside. For example, to access the configuration or event manager. Now we can use $this to get access to Phalcon\Di , as well as to already registered services.

Code before:

 use Phalcon\Mvc\Dispatcher; // ... $di->set( 'dispatcher', function () use ($di) { $eventsManager = $di->getEventsManager(); $eventsManager->attach( 'dispatch:beforeException', new NotFoundPlugin() ); $dispatcher = new Dispatcher; $dispatcher->setEventsManager($eventsManager); return $dispatcher; } ); 

Now you can access the services without transferring $di :

 use Phalcon\Mvc\Dispatcher; // ... $di->set( 'dispatcher', function () { $eventsManager = $this->getEventsManager(); $eventsManager->attach( 'dispatch:beforeException', new NotFoundPlugin() ); $dispatcher = new Dispatcher; $dispatcher->setEventsManager($eventsManager); return $dispatcher; } ); 

Service override allowed


If the object is returned after the beforeServiceResolve event in Phalcon\Di , the returned instance overrides the default service value.

The following example shows how to override the creation of the response service from its plugin:

 use Phalcon\Di; use Phalcon\Http\Response; use Phalcon\Events\Manager; use MyApp\Plugins\ResponseResolverInterceptor; $di = new Di(); $eventsManager = new EventsManager; // Intercept service creation $eventsManager->attach( 'di', new ResponseResolverInterceptor() ); $di->set('response', Response::class); $di->setInternalEventsManager($eventsManager); 

Using plugins, you can intercept the creation of services:

 namespace MyApp\Plugins; use Phalcon\Http\Response; class ResponseResolverInterceptor { private $cache = false; public function beforeServiceResolve($event, $di, $parameters) { // Intercept creation of responses if ($parameters['name'] == 'response' && $this->cache == false) { $response = new Response(); $response->setHeader('Cache-Control', 'no-cache, must-revalidate'); return $response; } } } 

Disabling the view from the action method


Sometimes there is a need to turn off the view by calling $this->view->disable() within a particular controller's action method in order to prevent Phalcon\Mvc\View further processing the result.

This has become much easier; just return false :

 use Phalcon\Mvc\Controller; class Api extends Controller { public function loginAction() { if ($this->safeApp->isBanned()) { $this->response->setStatusCode(401, "Unauthorized"); return false; } // ... } } 

Returning a string makes it the body of the response.


Returning the string from the controller action is taken as the response body:
(same as return $this->response->setContent('Hello world') )

 use Phalcon\Mvc\Controller; class Session extends Controller { public function welcomeAction() { return '<h1>Hello world!</h1>'; } } 

Especially useful if Phalcon\Mvc\View\Simple used instead of Phalcon\Mvc\View :

 use Phalcon\Mvc\Controller; class Session extends Controller { public function welcomeAction($name) { return $this->view->render( 'welcome/index', [ 'name' => $name, ] ); } } 

This feature is also available in Mvc\Micro handlers:

 use Phalcon\Mvc\Micro; $app = new Micro(); // ... $app->get( '/hello/{name}', function () { return $this->view->render( 'hello', [ 'name' => $name, ] ); } ); 

Redefining dispatcher behavior + views in routes


Routes can now be assigned callbacks that can override the default behavior of the manager and view:

 // Make a redirection if the /help route is matched $router->add('/help', [])->match(function () { return $this->getResponse()->redirect('https://support.google.com/'); }); // Return a string directly from the route $router->add('/', [])->match(function () { return '<h1>It works</h1>'; }); 

Read the full list of changes to Phalcon 2.1 at CHANGELOG .

Help with testing


This version can be installed from the 2.1. branch. If you do not have Zephir, run the following commands:

 git clone https://github.com/phalcon/cphalcon git checkout 2.1.x cd cphalcon/ext sudo ./install 

If you have Zephir installed:

 git clone https://github.com/phalcon/cphalcon cd cphalcon/ git checkout 2.1.x zephir build 

We hope you enjoy these improvements and additions. We invite you to share your thoughts and questions about this version on Phosphorum .

<3 Phalcon Team

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


All Articles