Skip to main content

PHP

Symfony Debug to Error Handler Component

·260 words·2 mins
Since symfony 4.4 the symfony/debug component is deprecated. The “\Symfony\Component\Debug\ErrorHandler” class is deprecated since Symfony 4.4, use “\Symfony\Component\ErrorHandler\ErrorHandler” Running unit tests on Mascot, I noticed lots of warnings. But it was not easy as changing Symfony\Component\Debug\ExceptionHandler to Symfony\Component\ErrorHandler\ErrorHandler. Assume we have something as below 37 38 39 40 41 42 43 44 <?php $handler = new \Symfony\Component\Debug\ExceptionHandler($debug); $exception = $event->getThrowable(); if (!$exception instanceof FlattenException) { $exception = FlattenException::create($exception); } $response = Response::create($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders())->setCharset(ini_get('default_charset')); $event->setResponse($response); The Symfony\Component\ErrorHandler\ErrorHandler has no getHtml method. From the symfony docs, if you are in development mode we can use below code.

Using CakePHP Migrations as a Standalone Library

·355 words·2 mins
Cakephp version 3 have a nice ORM. When using the cakephp/orm, it may be nice to integrate cakephp/migration than any other migration libraries, even though it uses phinx under the hood. Lets see how we can install and integrate cakephp/migration in our application. 1 composer require cakephp/migrations:dev-master The dev-master is currently passed for we need the latest version of master branch. Before this pull request, it was having dependency on cakephp/cakephp, which is not needed.

Building Modular Applications With PSR-7

·1152 words·6 mins
PSR-7, the HTTP message interfaces opened a new door of creating modular applications. Sadly many of the PSR-7 implementations added many helper methods. So if someone is creating a library that needs a PSR-7 implementations they tie the particular library with the PSR-7 implementation and use these convinient helper methods. So was PSR-15: interfaces for HTTP Middleware and PSR-17: interfaces for HTTP Factories was proposed. When creating a module one of the most challenging part is how to serve the javascript, css and images. We are going to use hkt/psr7-asset which is a fork of Aura.Asset_Bundle . What you want to do is only map the path to the assets folder.

Aura.Di 2.x to 3.x Upgrade Guide

·251 words·2 mins
3.x has a very minimal BC break. But if you are not sure what are they, then you may feel the pain. I am trying to document most of them, incase I missed please edit and send a pull request. I will try to eventually pushed to the main Aura.Di repo. BC Breaks # Instantiation # The way di container is instantiated has been changed from

CakePHP ORM and Logging Queries

·196 words·1 min
Working with cakephp/orm library, I needed to log all the queries. Cakephp provides a way to do it via cakephp/log. 1 2 3 4 5 6 7 8 9 <?php use Cake\Log\Log; Log::config('queries', [ 'className' => 'File', 'path' => '/my/log/path/', 'file' => 'app', 'scopes' => ['queriesLog'] ]); But you are not limited, if you need to configure it to a PSR-3 logger like monolog/monolog 1 2 3 4 5 6 7 8 9 10 <?php use Cake\Log\Log; use Monolog\Logger; use Monolog\Handler\StreamHandler; Log::config('default', function () { $log = new Logger('cli'); $log->pushHandler(new StreamHandler('php://stdout')); return $log; }); That was pretty simple and it logs to cli.

CakePHP ORM and Illuminate Pagination

·304 words·2 mins
Do you know CakePHP version 3 has a lovely ORM which can be used as standalone? Thank you José Lorenzo Rodríguez and every contributor, for your hard work. 1 composer require cakephp/orm That’s it. Working on I noticed I need to do some pagination. Oh, remember we have illuminate/pagination. Why not use it? Problem, there seems no one have implemented it. How could we achieve it? Lets do it. 1 composer require illuminate/pagination If you are using a psr-7 request / response here is the middleware for you.

Integrating Zend Form in Zend Expressive and Zend View

Example is based using Aura.Di. But the functionality will be same for any containers. First register the service Zend\View\HelperPluginManager, so that we can access the same object. To register the form helpers, create the object of Zend\Form\View\HelperConfig and pass the Zend\View\HelperPluginManager service. Example code with Aura.Di version 3 configuration. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php use Aura\Di\ContainerConfig; use Aura\Di\Container; use Zend\Form\View\HelperConfig; class ViewHelper extends ContainerConfig { public function define(Container $di) { $di->set('Zend\View\HelperPluginManager', $di->lazyNew('Zend\View\HelperPluginManager')); } public function modify(Container $di) { $serviceManager = $di->get('Zend\View\HelperPluginManager'); $helper = new HelperConfig(); $helper->configureServiceManager($servicemanager); } } Creating your own zend-view helper # Create your helper class 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php namespace App\View\Helper; use Zend\View\Helper\AbstractHelper; class HasError extends AbstractHelper { // add as many parameters you want to pass from the view public function __invoke() { // some code } } Registering your helper class. First get the Zend\View\HelperPluginManager service.

Eloquent and Pagination Inside Zend Expressive

·154 words·1 min
Recently working with eloquent (Laravel’s orm), zend expressive and zend view, I wanted to integrate pagination. It was simple as registering a Paginator middleware. 1 2 3 4 5 6 7 8 9 10 11 12 <?php use Illuminate\Pagination\Paginator; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; IlluminatePaginator::currentPageResolver(function ($pageName) use ($request) { $params = $request->getQueryParams(); return empty($params[$pageName]) ? 1 : $params[$pageName]; }); IlluminatePaginator::currentPathResolver(function () use ($request) { return $request->getUri()->getPath(); }); and you can call paginate on the Model. Eg : Consider you have a Post model.

Custom Events in a Symfony2 Bundle

·301 words·2 mins
In this tutorial we will create a custom event for symfony2 bundle. Assuming you have downloaded the symfony-standard distribution to play. Create HktEventBundle via sensio generator bundle. 1 2 <?php php app/console generate:bundle --namespace=Hkt/EventBundle --dir src --no-interaction Create the event class. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?php // src/Hkt/EventBundle/Event/PageViewed.php namespace Hkt\EventBundle\Event; use Symfony\Component\EventDispatcher\Event; class PageViewed extends Event { protected $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } Add as many methods/properties which are needed from the listener. Instead of creating an event class we can make use of generic event also.

20 Years of PHP

·302 words·2 mins
First my apologies being late! I hope you all know PHP is celebrating its 20th years of existence in the web. There is something that makes PHP unique that helps to standout with other languages. In this opporchunity I would like to thank the creator, the contributors, the maintainers, and all the users (past/present) to make this memorable. My Story # I got an exposure to learn and use internet in my college days. In those period I look html as a very complex language. I wasn’t aware there exists editors which help you.