Saturday, September 7, 2013

How to Create a Custom ZF2 ServiceManager Plugin?

If you want to discover how a ServiceManager Plugin gets initialized go here:
http://www.franzdeleon.me/2013/09/how-does-servicemanager-plugin-get.html

So how do we register our own custom ServiceManager Plugin?

1.) First create a Plugin Manager:

use Zend\ServiceManager\AbstractPluginManager;
class CustomPluginManager extends AbstractPluginManager
{
    public function validatePlugin($plugin)
    {
        if (!$plugin instanceof CustomeInterface) {
            throw new ErrorException('Wrong intance of plugin');
        }
    }
}
2.) Then create a ServiceManager Factory that returns an instance of your just created CustomPluginManager:

use Zend\Mvc\Service\AbstractPluginManagerFactory;
class CustomManagerFactory extends AbstractPluginManagerFactory
{
    const PLUGIN_MANAGER_CLASS = 'Namespace\to\CustomPluginManager';
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $plugins = parent::createService($serviceLocator);
        return $plugins;
    }
}
3.) Now lets define an interface for our plugin config:

interface CustomPluginProviderInterface
{
    public function getCustomPlugin();
}

4.) Then we need to put this all together by registering our CustomPlugin plugin in Module.php. We first need to register our Factory:

//inside Module.php
public function getServiceConfig()
{
    return array(
        'factories' => array(
            'customPluginManager' => 'namespace\to\CustomManagerFactory',
        ),
    )
}
5.) In order for the Plugin to be created as a plugin manager, we need to register it to the Service Listener. We do this via init() method. It is important to put this inside init() as this is called before the MVC bootstrapping.

// inside Module.php
public function init(ModuleManager $moduleManager)
{
    $sm = $moduleManager->getEvent()->getParam('ServiceManager');
    $serviceListener = $sm->get('ServiceListener');
    $serviceListener->addServiceManager(
        'customPluginManager',
        'custom_plugin_config',
        'namespace\to\CustomPluginProviderInterface',
        'getCustomPluginConfig'
    );
}
6.) Now you can register to your custom plugin from other modules!
// in some other Module.php
public function getCustomPluginConfig()
{
    return array(
        'invokables' => array(
            'AclService' => 'namespace/to/AclService'
        )
    )
}
7.) Then you can call it for example inside a controller:
 
// inside controller.php in some other module
public function someAction()
{
    $aclService = $this->getServiceLocator()->get('customPluginManager')->get('AclService');
}

That is pretty much it!

Reference:
http://raing3.gshi.org/2013/05/26/creating-custom-plugin-manager-in-zend-framework-2/

26 comments:

Anonymous said...

Thank you! You saved my time.

Unknown said...

Internet web marketing can make the world your oyster, that is, if you have a good plan, preparation and persistence in mind.
webmarketing

Unknown said...

2015-09-16 zhengjx
Ralph Lauren UK Sale Polo Shirts
Christian Louboutin Outlet For Women
ugg boots
Louis Vuitton Outlet Authentic Handbags Discount
michael kors handbags
Louis Vuitton Handbags Outlet On Sale
Louis Vuitton Outlet Purses and Handbags For Sale
canada goose
hermes birkin
New Jordans Cheap For Sale
Abercrombie & Fitch Clothing With Big Discount
Coach Factory Outlet Handbags 70% OFF
Lebron 12,11,10 Shoes For Sale
cheap uggs sale
Michael Kors Outlet Online Sale Free Shipping
Louis Vuitton Handbags Outlet Sale
Air Jordan 4 Retro Free Shipping
coach outlet store online
Louis Vuitton Handbags Outlet Stores Online
Ray Ban Sunglasses Outlet Store Online
ugg boots
michael kors handbag
michael kors outlet
Abercrombie And Fitch New York Clothing Warehouse
ralph lauren uk
Cheap Oakley Sunglasses Outlet With Discount
tory burch outlet
nike air max
Coach Outlet Store Online Free Shipping
Michael Kors Outlet USA Store

Unknown said...

cheng0928
louis vuitton handbags, http://www.louisvuittonhandbag.us/
hermes birkin, http://www.hermesbirkin.org/
mbt shoes, http://www.mbtshoesoutlet.us.com/
swarovski outlet, http://www.swarovski-outlet.co.uk/
hogan, http://www.scarpehogan-outlet.it/
ray ban, http://www.occhiali-rayban.it/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
nba jerseys, http://www.nbajerseys.us.com/
longchamp handbags, http://www.longchamphandbag.us.com/
cheap nhl jerseys, http://www.nhljerseys.us.com/
cheap mlb jerseys, http://www.cheapmlbjerseys.net/
north face jackets, http://www.thenorthfaces.in.net/
cheap snapbacks, http://www.cheapsnapbacks.us.com/
adidas wings, http://www.adidaswings.net/
basketball shoes, http://www.basketballshoes.us.com/
lebron james shoes, http://www.lebronjamesshoes.in.net/
lacoste polo shirts, http://www.lacostepoloshirts.cc/
vans shoes, http://www.vans-shoes.cc/
ugg boots, http://www.uggboot.com.co/
hermes belt, http://www.hermesbelts.us/

oakleyses said...

tory burch outlet, longchamp outlet, burberry, ray ban sunglasses, tiffany and co, nike outlet, replica watches, louis vuitton, ray ban sunglasses, nike air max, jordan shoes, polo ralph lauren outlet, michael kors outlet, tiffany jewelry, michael kors outlet, christian louboutin outlet, burberry outlet online, michael kors outlet, michael kors, oakley sunglasses, longchamp, prada outlet, prada handbags, cheap oakley sunglasses, oakley sunglasses, oakley sunglasses, ugg boots, louboutin shoes, michael kors outlet, ugg boots, polo ralph lauren outlet, louis vuitton outlet, nike free, chanel handbags, louboutin outlet, louis vuitton outlet, oakley sunglasses, ugg boots, replica watches, michael kors outlet, kate spade outlet, uggs on sale, nike air max, louboutin, louis vuitton, louis vuitton

oakleyses said...

air jordan pas cher, ray ban uk, nike roshe, true religion jeans, air force, abercrombie and fitch, north face, new balance pas cher, sac longchamp, longchamp pas cher, mulberry, oakley pas cher, nike free run uk, coach outlet, nike blazer, hermes, michael kors, nike air max, air max, hollister pas cher, true religion jeans, ralph lauren pas cher, vans pas cher, michael kors, coach outlet, true religion jeans, hollister, michael kors, north face, nike air max, nike roshe run, coach purses, burberry, lululemon, michael kors, lacoste pas cher, hogan, tn pas cher, sac guess, replica handbags, louboutin pas cher, vanessa bruno, timberland, kate spade handbags, converse pas cher, ralph lauren uk, nike free

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

nike blazer pas cher
reebok outlet store
adidas nmd runner
longchamp handbags
adidas uk
uggs outlet
abercrombie and fitch
michael kors handbags
coach outlet
michael kors handbags
michael kors outlet online
ray ban sunglasses
longchamp solde
prada sunglasses
swarovski jewelry
hollister uk
ugg boots
football shirts
ugg australia
canada goose jackets
cheap ugg boots
lacoste pas cher
uggs outlet
michael kors outlet online
michael kors handbags
toms shoes
uggs outlet
coach outlet
coach outlet clearance
dior sunglasses
adidas wings
lululemon outlet
louis vuitton outlet
the north face jackets
true religion uk
chanyuan0913

Unknown said...

tiffany and co
louis vuitton outlet
clarisonic
true religion jeans
timberland outlet
true religion outlet
louis vuitton outlet
cheap uggs
coach outlet
true religion outlet
mlb jerseys
oakley sunglasses
adidas outlet
michael kors outlet
longchamp handbags
beats by dre
rolex watches
ray ban sunglasses
ugg boots
ugg australia
moncler uk
ray ban sunglasses
louis vuitton paris
toms shoes
louis vuitton handbags
coach factory outlet
moncler uk
gucci outlet
canada goose outlet
adidas nmd
timberland boots
juicy couture
burberry outlet
ugg boots
the north face jackets
timberland boots
new york knicks jerseys
nike outlet store
ugg sale
ugg italia
201611.5wengdongdong

raybanoutlet001 said...

mont blanc pens outlet
vikings jerseys
nike huarache
converse trainers
armani exchange outlet
oklahoma city thunder
hollister clothing
michael kors handbags
toms shoes
ravens jerseys

Unknown said...

michael kors handbags wholesale
rolex replica watches
michael kors handbags
basketball shoes
hugo boss
cheap oakley sunglasses
boston celtics
cheap jordans
converse trainers
ferragamo shoes

caiyan said...

polo ralph lauren outle
ray ban sunglasses cheap
adidas nmd r1
michael kors outlet store
rolex replica watches for sale
michael kors uk
pandora jewellery
fit flops
kate spade
ralph lauren uk
0413shizhong

Unknown said...

under armour shoes
pandora jewelry outlet
michael kors handbags outlet
christian louboutin
air jordan shoes
michael kors handbags
coach factory outlet
pandora bracelet
longchamp sale
christian louboutin shoes
2017.6.27chenlixiang

Unknown said...

adidas outlet
nike air max 90
gucci outlet
ugg boots
timberland boots
nike store
pandora jewelry
yeezy boost 350
fitflops shoes
polo outlet
mt0718

Unknown said...

nike tn
omega watches sale
ugg outlet
ugg outlet
ugg outlet
coach outlet store
prada outlet
jordan 4
michael kors purses
coach outlet store online

Digital Marketing Agency said...

Quikcbooks Support Phone Number

Quikcbooks Support Number
Quikcbooks Support Phone Number
Quikcbooks Tech Support Number
Quikcbooks Technical Support Number
Quikcbooks Tech Support Phone Number
Quikcbooks Technical Support Phone Number
Quikcbooks Helpdesk Phone Number
Quikcbooks Customer Service Phone Number
Quikcbooks Customer Service Number
Quikcbooks Customer Support Number

Quicken Support Phone Number

Quicken Support Number
Quicken Support Phone Number
Quicken Tech Support Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Technical Support Phone Number
Quicken Helpdesk Phone Number
Quicken Customer Service Phone Number
Quicken Customer Service Number
Quicken Customer Support Number

Sage Support Phone Number

Sage Support Number
Sage Support Phone Number
SageTech Support Number
Sage Technical Support Number
Sage Tech Support Phone Number
Sage Technical Support Phone Number
Sage Helpdesk Phone Number
Sage Customer Service Phone Number
Sage Customer Service Number
Sage Customer Support Number

Unknown said...

mulberry
michael kors outlet canada
michael kors outlet clearance
north face outlet
ugg boots
tory burch outlet online
coach outlet online
michael kors outlet store
birkin handbags
uggs on sale
mt1225

Foogle Packers and Movers said...

Top 15 Packers and Movers
Top 15 Packers and Movers in Thane
Top 15 Packers and Movers in Noida
Top 15 Packers and Movers in Kolkata
Top 15 Packers and Movers in Faridabad
Top 15 Packers and Movers in Ghaziabad
Top 15 Packers and Movers in Chandigarh
Top 15 Packers and Movers in Navi Mumbai

Unknown said...

qzz0510
nike roshe one
salomon shoes
kate spade outlet
timberwolves jerseys
adidas uk
76ers jerseys
polo ralph lauren
michael kors
michael kors outlet
nike store

koko said...

nike factory store
christian louboutin outlet
dsquared clothing
nike sale
nike presto
coach outlet store online
christian louboutin shoes
nfl jerseys
canada goose jackets
carolina panthers jersey

Unknown said...

jordan 4
nike huarache
adidas nmd
true religion outlet
michael kors handbags
nike air max
goyard handbags
nike air max 270
kobe shoes
longchamp handbags

5689 said...

zzzzz2018.11.6
moncler jacket
michael kors outlet
ralph lauren
nba jerseys
coach outlet
pandora jewelry
moncler outlet
pandora
jordan uk
canada goose outlet

noor said...

من جدة الى الاردن شركة شحن عفش من جدة الى الاردن افضل شركات شحن اثاث من جدة الى الاردن
نقل عفش من الدمام الى الاحساء نقل عفش من الدمام الى الاحساء