Phalcon\Mvc\Model\Validation
been deprecatedPhalcon\Mvc\Model\Validation
replaced by Phalcon\Validation
. The functionality of both components merges into one, thereby simplifying the support of the code base. 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; } } }
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(); } }
Phalcon\Mvc\Model
$customer = new Customer( [ 'name' => 'Peter', 'status' => 'Active', ] );
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. 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.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$this
to get access to Phalcon\Di
, as well as to already registered services. 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; } );
$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; } );
beforeServiceResolve
event in Phalcon\Di
, the returned instance overrides the default service value.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);
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; } } }
action
method$this->view->disable()
within a particular controller's action
method in order to prevent Phalcon\Mvc\View
further processing the result.false
: use Phalcon\Mvc\Controller; class Api extends Controller { public function loginAction() { if ($this->safeApp->isBanned()) { $this->response->setStatusCode(401, "Unauthorized"); return false; } // ... } }
return $this->response->setContent('Hello world')
) use Phalcon\Mvc\Controller; class Session extends Controller { public function welcomeAction() { return '<h1>Hello world!</h1>'; } }
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, ] ); } }
Mvc\Micro
handlers: use Phalcon\Mvc\Micro; $app = new Micro(); // ... $app->get( '/hello/{name}', function () { return $this->view->render( 'hello', [ 'name' => $name, ] ); } );
// 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>'; });
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
git clone https://github.com/phalcon/cphalcon cd cphalcon/ git checkout 2.1.x zephir build
Source: https://habr.com/ru/post/266521/
All Articles