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/

28 comments:

Anonymous said...

Thank you! You saved my time.

Rosen Rose said...

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

Zheng junxai5 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

Wenhao Guo 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/

mmjiaxin said...

cleveland cavaliers jersey, http://www.cavaliersjerseys.net/
north face outlet store, http://www.thenorthfaceoutletstores.org/
supra footwear, http://www.suprashoes.us.com/
tods shoes,tods shoes sale,tods sale,tods outlet online,tods outlet store,tods factory outlet
oakley sunglasses wholesale, http://www.wholesaleoakleysunglasses.us.com/
tiffany outlet, http://www.tiffanyjewelleryoutlets.co.uk/
true religion outlet, http://www.truereligionjean.in.net/
chicago bulls jersey, http://www.chicagobullsjerseys.net/
denver broncos jerseys, http://www.denverbroncos.org/
los angeles clippers, http://www.clippersjerseystore.com/
iphone cases, http://www.iphonecase.name/
colts jerseys, http://www.indianapoliscoltsjersey.us/
falcons jersey, http://www.atlantafalconsjersey.us/
insanity, http://www.insanityworkout.us.com/
fred perry polo shirts, http://www.fredperrypoloshirts.com/
ray ban sunglasses outlet, http://www.raybansunglass.us.com/
ray ban outlet, http://www.raybansunglassesonline.in.net/
packers jerseys, http://www.greenbaypackersjersey.us/
true religion outlet, http://www.truereligionjeansoutlet.com/
hermes belt for sale, http://www.hermesbelts.us/
mm1007

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

oakleyses said...

nike roshe, new balance, beats by dre, p90x workout, instyler, valentino shoes, nfl jerseys, babyliss, ghd, hollister, s5 cases, lululemon, soccer shoes, jimmy choo shoes, nike air max, mac cosmetics, giuseppe zanotti, abercrombie and fitch, hollister, herve leger, asics running shoes, nike huarache, celine handbags, louboutin, mont blanc, iphone 6s cases, north face outlet, birkin bag, iphone 5s cases, north face outlet, wedding dresses, mcm handbags, oakley, iphone 6 cases, ralph lauren, longchamp, timberland boots, nike trainers, baseball bats, soccer jerseys, iphone 6s plus cases, insanity workout, iphone 6 plus cases, iphone cases, bottega veneta, chi flat iron, reebok shoes, ipad cases, vans shoes, ferragamo shoes

oakleyses said...

moncler, moncler outlet, marc jacobs, moncler, hollister, montre pas cher, vans, ray ban, replica watches, pandora charms, louis vuitton, supra shoes, sac louis vuitton pas cher, pandora jewelry, swarovski, pandora charms, moncler, bottes ugg, barbour, canada goose, ugg,uggs,uggs canada, louis vuitton, doke gabbana outlet, converse, louis vuitton, lancel, louis vuitton, hollister, moncler, pandora jewelry, doudoune canada goose, juicy couture outlet, nike air max, converse outlet, barbour jackets, moncler, ugg,ugg australia,ugg italia, links of london, moncler, gucci, canada goose outlet, canada goose, canada goose outlet, coach outlet, canada goose uk, ugg boots uk, canada goose, juicy couture outlet, ugg pas cher, wedding dresses, canada goose

方松腾 said...

bottega veneta outlet online
burberry outlet
cheap toms
polo ralph lauren outlet
coach outlet store online,coach outlet store,coach outlet
nba jerseys wholesale
moncler outlet
ugg boots clearance
valentino shoes
michael kors factory sale
rolex watches
kobe bryants shoes 2015
mont blanc
true religion jeans,cheap true religion jeans,true religion jeans wholesale,true religion outlet,true religion sale,true religion outlet online
ray-ban sunglasses
ysl outlet online
louis vuitton outlet
lebron james shoes,lebron 12,lebron 12 shoes,lebron sneakers,lebron iv,lebron 11,lebron 10
hermes outlet online
cheap oakley sunglasses
celine outlet
cleveland cavaliers
football shirts uk,soccer jerseys uk,cheap soccer jerseys uk
ferragamo shoes
pandora charms
ralph lauren,ralph lauren uk,ralph lauren outlet,ralph lauren outlet online,polo ralph lauren outlet,ralph lauren polo
omega watches
michael kors uk outlet
burberry outlet
christian louboutin

mohit rao kumar said...

My Car Carrier from chandigarh to ranchi cost
My Car Carrier from lucknow to ranchi cost
My Car Carrier from Kolkata to ranchi cost
My Car Carrier from Hyderabad to ranchi cost
My Car Carrier from Bangalore to ranchi cost
My Car Carrier from pune to ranchi
My Car Carrier from Mumbai to ranchi cost
My Car Carrier from Chennai to ranchi cost
My Car Carrier from delhi to ranchi
My Car Carrier from Gurgaon to ranchi cost
My Car Carrier from Noida to ranchi
My Car Carrier from Faridabad to ranchi cost
My Car Carrier from Ahmedabad to ranchi cost
My Car Carrier from navi Mumbai to ranchi cost
My Car Carrier from thane to ranchi cost

mohit rao kumar said...

Packers And Movers Ahmedabad
Packers And Movers Surat
Packers And Movers vadodara
Packers And Movers chandigarh
Packers And Movers Amritsar
Packers And Movers panchkula
Packers And Movers Allahabad
Packers And Movers Kanpur
Packers And Movers agra
Packers And Movers Bangalore

mohit rao kumar said...

Movers And Packers panipat
Movers And Packers ambala
Movers And Packers rohtak
Movers And Packers hisar
Movers And Packers karnal
Movers And Packers bhiwani
Movers And Packers sirsa
Movers And Packers jind
Movers And Packers mysore
Movers And Packers nashik
Movers And Packers nagpur
Movers And Packers goa
Movers And Packers shillong
Movers And Packers guwahati
Movers And Packers jaipur
Movers And Packers jodhpur
Movers And Packers dehradun
Movers And Packers shimla
Movers And Packers siliguri
Packers And Movers ranchi
Movers And Packers jamshedpur
Movers And Packers kalyan

mohit rao kumar said...

Movers And Packers bhopal
Movers And Packers aurangabad
Movers And Packers meerut
Movers And Packers vasai virar
Movers And Packers salem
Movers And Packers guntur
Movers And Packers bhilai
Movers And Packers bhavnagar
Movers And Packers durgapur
Movers And Packers nellore
Movers And Packers erode
Movers And Packers gaya
Movers And Packers udaipur
Movers And Packers solapur
Movers And Packers amaravati
Movers And Packers gandhidham
Movers And Packers jamnagar
Movers And Packers gandhinagar
Movers And Packers vapi
Movers And Packers dindigul
Movers And Packers dhanbad
Movers And Packers sonipat

Narender Godara said...

Movers and packers chennai to hyderabad charges
Movers and packers coimbatore to indore charges
Movers and packers dehradun to lucknow charges
Movers and packers ahmedabad to vadodara charges
Movers and packers chandigarh to amritsar charges
Movers and packers allahabad to lucknow charges
Movers and packers kanpur to allahabad charges
Movers and packers lucknow to allahabad charges
Movers and packers cochin to coimbatore charges

Yuanyuan Lin said...

6.29llllllyuancheap nfl jersey
chrome hearts outlet
true religion outlet
true religion jeans
tory burch outlet
nike air huarache
nike huarache
cartier sunglasses
michael kors outlet
ferragamo shoes
coach outlet online
christian louboutin outlet
beats headphones
louis vuitton handbags
louis vuitton pas cher
true religion outlet
tiffany outlet
ralph lauren pas cher
herve leger outlet
christian louboutin shoes
christian louboutin uk
timberland shoes
michael kors canada
michael kors factory store
nba jerseys
tiffany outlet
basketball shoes
toms shoes
air jordan shoes for sale
adidas wings
mulberry handbags
ralph lauren outlet
ferragamo shoes
michael kors handbags
ralph lauren polo
6.29

John said...

basketball shoes
nhl jerseys wholesale
coach outlet store online
pandora jewelry outlet
yeezy boost 350
ralph lauren uk
cheap nfl jerseys
kobe bryant shoes
adidas gazelle
rolex watches uk
gucci handbags outlet
wholesale nike shoes
michael kors outlet clearance
cheap ray bans
adidas nmd
ray ban sunglasses
nike cortez white
ed hardy uk
supra shoes
coach outlet store online
north face outlet
red bottoms outlet online
ghd flat iron
skechers shoes
timberland shoes
michael kors outlet online
abercrombie kids
adidas supercolor
asics gel nimbus
stephen curry shoes
louis vuitton outlet online
vans shoes
cartier watches for sale
tiffany jewelry
201671yuanyuan

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

xumeiqing said...

2016923meiqing
yeezy boost 350
jordan shoes
polo ralph lauren outlet online
true religion outlet
fitflops sale clearance
michael kors outlet online
gucci handbags
red bottoms outlet online
fit flops
pandora jewelry

兮兮 said...

20161021 junda
michael kors outlet
oakley sunglasses
ugg sale
juicy couture outlet
longchamp pas cher
tory burch outlet
true religion jeans
rolex watches
swarovski crystal
uggs outlet

dong dong23 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

roshini said...


Sage Support Phone Number
Sage 50 Tech Support Phone Number
Sage 50 Support Number
Sage 50 Technical Support Phone Number
Sage 50 Tech Support Number
Sage 50 Technical Support Phone Number
Sage 50 Technical Support Number
Sage 50 Customer Support Phone Number
Sage 50 Customer Service Number

Soni yadav said...

Aol Technical Support Phone Number
Aol Technical Support Number
Aol Tech Support Number
Aol Tech Support phone Number
Aol Support Phone Number
Aol Tech Support Number
Outlook Technical Support Phone Number
Outlook Technical Support Number
Outlook Tech Support Number
Outlook Tech Support Phone Number
Outlook Support Phone Number
Outlook Tech Support Number

Anjilo Jilo said...


Quicken Support Phone Number
Quicken Support Phone Number
Quicken Support Number
Quicken Tech Support Number
Quicken Tech Support Phone Number
Quicken Technical Support Number
Quicken Technical Support Phone Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Tech Support Number

mohan kumar said...

Quickbooks Support Phone Number
Quickbooks Pro Support Phone Number
Quickbooks Pro Tech Support Number
Quickbooks Pro Support Number
Quickbooks Pro Technical Support Number
Quickbooks Pro Tech Support Phone Number
Quickbooks Pro Technical Support Phone Number
Quickbooks Pro Help Desk Phone Number
Quickbooks Pro Help Desk Number
Quickbooks Pro Customer Service Number
Quickbooks Pro Customer Service Phone Number
Quickbooks Pro Customer Support Number
Quickbooks Pro Customer Support Phone Number

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

Kanika Jaiswal said...

QuickBooks Technician
Quickbooks Support Phone Number
Quickbooks Tech Support Number
Quickbooks Support Number
Quickbooks Technical Support Number
Quickbooks Tech Support Phone Number
Quickbooks Technical Support Phone Number
Quickbooks Customer Service Number
Quickbooks Customer Service Phone Number
Quickbooks Customer Support Number
Quickbooks Customer Support Phone Number
Quickbooks Help Desk Phone Number
Quickbooks Help Desk Number

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