Skip to main content

CakePHP ORM and Illuminate Pagination

·304 words·2 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Illuminate\Pagination\Paginator as IlluminatePaginator;
class PaginatorMiddleware
{
    public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next = null )
    {
        IlluminatePaginator::currentPageResolver(function ( $pageName = 'page' ) use($request )
        {
            $params = $request->getQueryParams();
            return empty($params[$pageName]) ? 1 : $params[$pageName];
        });

        IlluminatePaginator::currentPathResolver(function () use($request )
        {
            return $request->getUri()->getPath();
        });

        return $next($request, $response);
    }
}

What we did above are a few things for Illuminate to give the url path when it is doing the pagination. So for example /article?page=<page-number> will come instead of just ?page=<page-number>.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
use Cake\ORM\TableRegistry;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

$table = TableRegistry::get('Articles');
$currentPage = 1;
$perPage = 20;
$query = $table->find('all');
$total = $query->count();
$items = $query->page($currentPage, $perPage);
$paginator = new LengthAwarePaginator($items, $total, $perPage, $currentPage, [
	'path' => Paginator::resolveCurrentPath(),
]);
echo $paginator->render();

The above code is querying the articles and rendering the pagination with the returned results.

By default the Presenter is Illuminate\Pagination\BootstrapThreePresenter, but you can create your own.

I hope you will love the integration.

Thank you everyone for your support and hard work on components to make PHP better every day.

Related

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.

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.

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.