Skip to main content

Integrating Zend Form in Zend Expressive and Zend View

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

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
#

  1. 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
    }
}
  1. Registering your helper class.

First get the Zend\View\HelperPluginManager service.

2.a ) Registering as a factory

1
2
3
4
<?php
$serviceManager->setFactory('hasError', function () {
    return new \App\View\Helper\HasError();
});

2.b ) As an invokable

1
2
<?php
$serviceManager->setInvokableClass('hasError', 'App\View\Helper\HasError');

Now you can access inside zend-view as $this->hasError(). If your view helper need dependencies don’t use the setInvokableClass method. Use factory and get the object from the container.

1
2
3
4
<?php
$serviceManager->setFactory('hasError', function () use ($di) {
     return $di->get('App\View\Helper\HasError');
});

I wished if $serviceManager can understand the aura’s lazyNew functionality so that we don’t need to register it as a service.

Eg : Below will not work.

1
2
<?php
$serviceManager->setFactory('hasError', $di->lazyNew('App\View\Helper\HasError'));

This is what I love to see it working for this a closure, but with namespaced.

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.

Speed Up Configuration in Aura v2

·322 words·2 mins
Aura v2 added auto resolution in-order to help lazy people writing configuration manually. Even though it was introduced to help, it introduced a few issues. So auto resolution will be disabled in the future. Some of the complains/suggestions are how to easily write the di configuration. So introducing you FOA.DiConfig Installation # 1 composer require foa/di-config Usage # 1 2 3 vendor/bin/di-config-dump Usage : vendor/bin/di-config-dump /real/path/to/file.php Usage : vendor/bin/di-config-dump /real/path/to/directory Example 1 # Let’s assume you have

Using Aura.Input Forms Inside Slim Framework

·738 words·4 mins
Rob Allen wrote about Integrating ZF2 forms into Slim. I did write how you can use Aura.Input and Aura.Html to create standalone form for PHP. This time I felt I should write about integrating aura input inside Slim. Let us install a few dependencies aura/input for building the form and aura/html for the html helpers. You of-course can skip not to use aura/html and build your own helper. I also purposefully left not integrating the powerful Aura.Filter , but you are not limited to integrate any validator you love inside Aura.Input .