Tag: laminas

  • Laminas delegator service

    Laminas delegator service

    Delegator service is a wrapper service which houses the “real” service. This is useful when you want to “decorate” your service, do some extra stuff without changing the working one. Laminas supports multi delegator services for each service, and it follows the nested object structure.

    This sample is base from this.

    Code

    The delegator service class, this will handle the stuff you want.

    namespace MyApp;
    
    class MyServiceDelegator extends \MyApp\MyService {
        protect $myservice;
    
        public function __construct(MyService $service) {
            $this->myservice = $service;
        }
    
        public function hello($name, $title = null) {
             if ($title == null) {
                 $title = 'Ms. ';
             }
             
             $result = $this->myservice->hello($title . $name);
    
             return $result;
        }
    }

    The delegator factory class

    namespace MyApp;
    
    class MyServiceDelegatorFactory implements Laminas\ServiceManager\Factory\DelegatorFactoryInterface\DelegatorFactoryInterface  {
         public function __invoke(\Interop\Container\ContainerInterface $container, $name, callable $callback, array $options = null) {
             return new \MyApp\MyServiceDelegator($callback());
         }
     }

    Configuration

    'service_manager' => [
        ///Other service configuration
        ///...
        'delegators' => [
            \MyApp\MyService::class => [
                \MyApp\MyServiceDelegator::class
            ]
        ]
    ]

    Usage

    $application->getServiceManager()->get(\MyApp\MyService::class);///this returns \MyApp\MyServiceDelegator instead of \MyApp\MyService

    Happy coding!

  • Custom service for Laminas web app

    Custom service for Laminas web app

    Follow these simple steps to create a custom service for Laminas:

    Code

    namespace MyApp;
    
    class MyService {
        public function hello($name) {
            return 'Hola ' . $name;
        }
    }

    Configuration

    'service_manager' => [
        'factories' => [
            \MyApp\MyService::class => \Laminas\ServiceManager\Factory\InvokableFactory::class
        ]
    ]

    Note

    You can create your own factory class so as your object can access the service manager or do some special prepare for thereof. To make a factory class, just create a class with postfix Factory and implements from interface FactoryInterface.

    namespace MyApp;
    
    class MyServiceFactory implements \Laminas\ServiceManager\FactoryInterface {
        public function __invoke(\Interop\Container\ContainerInterface ContainerInterface $container, $requestedName, array $options = null) {
            ///$container is the service manager
            
            return new MyService();
        }
    }

    Usage

    Now, you can access it via service manager.

    $application->getServiceManager()->get(\MyApp\MyService::class);

    Happy coding!

  • Laminas View Helper

    Laminas View Helper

    No need to say, just follow these steps below:

    Code

    namespace MyApp;
    
    class MyHelper extends \Laminas\View\Helper\AbstractHelper {
        public function __invoke() {
            return $this;
        }
    
        public function say() {
            return 'Bonjour!';
        }
    }

    Configuration

    'view_helpers' => [
        'aliases' => [
            'myhelper' => \MyApp\MyHelper::class
        ],
        'factories' => [
            \MyApp\MyHelper::class => \Laminas\ServiceManager\Factory\InvokableFactory::class
        ]
    ]

    Note

    You can create your own factory class so as your object can access the service manager or do some special prepare for thereof. To make a factory class, just create a class with postfix Factory and implements from interface FactoryInterface.

    namespace MyApp;
    
    class AbcFactory implements \Laminas\ServiceManager\FactoryInterface {
        public function __invoke(\Interop\Container\ContainerInterface ContainerInterface $container, $requestedName, array $options = null) {
            ///$container is the service manager
            
            return new MyHelper();
        }
    }

    Usage

    In view scripts:

    $this->myhelper();//return MyHelper object
    $this->myhelper()->say();//quick access to member function

    In other view helpers:

    $this->view->myhelper();//return MyHelper object
    $this->view->myhelper()->say();//quick access to member function

    Happy coding!

  • Laminas Controller Plugin

    Laminas Controller Plugin

    Say no more…

    Code

    namespace MyApp;
    
    class MyPlugin extends \Laminas\Mvc\Controller\Plugin\AbstractPlugin {
        public function __invoke() {
            return $this;
        }
    
        public function say() {
            return 'Hello!';
        }
    }

    Configuration

    'controller_plugins' => [
        'aliases' => [
            'myplugin' => \MyApp\MyPlugin::class
        ],
        'factories' => [
            \MyApp\MyPlugin::class => \Laminas\ServiceManager\Factory\InvokableFactory::class
        ]
    ]

    Note

    You can create your own factory class so as your object can access the service manager or do some special prepare for thereof. To make a factory class, just create a class with postfix Factory and implements from interface FactoryInterface.

    namespace MyApp;
    
    class MyPluginFactory implements \Laminas\ServiceManager\FactoryInterface {
        public function __invoke(\Interop\Container\ContainerInterface ContainerInterface $container, $requestedName, array $options = null) {
            ///$container is the service manager
            
            return new MyPlugin();
        }
    }

    Usage

    In any controller:

    $this->myplugin();//return object MyPlugin
    $this->myplugin()->say();//quick access to a member function

    Happy coding!