Tuesday, August 27, 2013

TIP: How to Immitate init() inside Controllers in ZF2

For ZF1 users there is the convenient init() method that you can use on every controller but in ZF2 the init() does not exist anymore for design purposes.

There are sometimes advantages though of having an init() type functionality inside the constructor. For example, checking the ACL.

A little background. One of the problems of working directly inside the constructor in ZF2 is because the dispatch event has not occured yet, you cannot grab important objects like controller plugins so doing the bellow wont work.
// assume we are inside a controller
public function __construct()
{
    $this->redirect()->toRoute('someroute'); // I will not work!
}
Solution:
There are tons of ways to do this but I find this a little easier:
class IndexController extends AbstractActionController
{
    public function __construct()
    {
        $resource = $this->getResourceId();
        $this->getEventManager()->attach('dispatch', function ($e) use ($resource) {
            $controller = $e->getTarget(); // this will return your controller instance

            // there is no ACL plugin ok! I created that!
            if (!$controller->acl()->isAllowed('me', $resource)) {
                // now i can use controller plugins!
                return $controller->redirect()->toRoute('authenticate');
            }     
        });
    }

    public function getResourceId()
    {
        return 'some_resource';
    }
}
Reference:
http://mwop.net/blog/2012-07-30-the-new-init.html

33 comments:

Coach Factory said...

moncler outlet, cheap oakley sunglasses, canada goose outlet, oakley outlet, ugg boots, ray ban outlet, oakley sunglasses cheap, ray ban, ray ban outlet, ray-ban, oakley sunglasses outlet, oakley outlet, cheap oakley sunglasses, oakley vault, ray ban sunglasses outlet, canada goose, oakley vault, louboutin, tn pas cher, cheap oakley sunglasses, ray ban, canada goose, ray ban, ugg, oakley sunglasses outlet, ray bans, raybans, canada goose, oakley vault, ray-ban, oakley outlet, cheap sunglasses, ralph lauren, rayban, cheap oakley, moncler, oakley sunglasses cheap, gucci, oakley sunglasses outlet, moncler, oakley, canada goose, cheap sunglasses, ray bans, cheap sunglasses, oakley sunglasses cheap, moncler, canada goose outlet, rayban, discount oakley sunglasses

Coach Factory said...

moncler outlet, cheap oakley sunglasses, canada goose outlet, oakley outlet, ugg boots, ray ban outlet, oakley sunglasses cheap, ray ban, ray ban outlet, ray-ban, oakley sunglasses outlet, oakley outlet, cheap oakley sunglasses, oakley vault, ray ban sunglasses outlet, canada goose, oakley vault, louboutin, tn pas cher, cheap oakley sunglasses, ray ban, canada goose, ray ban, ugg, oakley sunglasses outlet, ray bans, raybans, canada goose, oakley vault, ray-ban, oakley outlet, cheap sunglasses, ralph lauren, rayban, cheap oakley, moncler, oakley sunglasses cheap, gucci, oakley sunglasses outlet, moncler, oakley, canada goose, cheap sunglasses, ray bans, cheap sunglasses, oakley sunglasses cheap, moncler, canada goose outlet, rayban, discount oakley sunglasses

Coach Factory said...

jordan shoes, louboutin, michael kors outlet, michael kors, michael kors, louis vuitton handbags, nike air max, louis vuitton, gucci outlet, true religion outlet, michael kors outlet, air max, coach outlet store, ray ban sunglasses, burberry outlet online, michael kors, longchamp outlet, nike free, true religion jeans, burberry outlet online, coach factory outlet, louboutin, toms shoes, air jordan, polo ralph lauren, chanel handbags, ray ban, prada outlet, tiffany and co, nike air max, polo ralph lauren outlet, louboutin, coach outlet, nike shoes, ralph lauren, kate spade outlet, tory burch outlet, air max, oakley sunglasses, oakley sunglasses, true religion, michael kors outlet, oakley sunglasses, longchamp, prada handbags, nike free, coach purses, coach factory,

Coach Factory said...

jordan shoes, louboutin, michael kors outlet, michael kors, michael kors, louis vuitton handbags, nike air max, louis vuitton, gucci outlet, true religion outlet, michael kors outlet, air max, coach outlet store, ray ban sunglasses, burberry outlet online, michael kors, longchamp outlet, nike free, true religion jeans, burberry outlet online, coach factory outlet, louboutin, toms shoes, air jordan, polo ralph lauren, chanel handbags, ray ban, prada outlet, tiffany and co, nike air max, polo ralph lauren outlet, louboutin, coach outlet, nike shoes, ralph lauren, kate spade outlet, tory burch outlet, air max, oakley sunglasses, oakley sunglasses, true religion, michael kors outlet, oakley sunglasses, longchamp, prada handbags, nike free, coach purses, coach factory,

Coach Factory said...

celine handbags, chi flat iron, nike air max, nfl jerseys, ugg boots, lululemon, mcm handbags, marc jacobs, supra shoes, soccer shoes, lancel, beats by dre, vans, p90x3, ghd, herve leger, nike free, ferragamo shoes, burberry, birkin bag, juicy couture outlet, montre pas cher, soccer jerseys, ralph lauren, hermes, north face outlet, ugg boots, valentino shoes, louis vuitton, jimmy choo, new balance shoes, mulberry, rolex watches, louis vuitton, abercrombie and fitch, oakley pas cher, reebok outlet, hollister, air max, mont blanc, bottega veneta, converse shoes, ray ban pas cher, karen millen, wedding dresses, hogan, yoga pants, insanity, asics gel, timberland

Coach Factory said...

celine handbags, chi flat iron, nike air max, nfl jerseys, ugg boots, lululemon, mcm handbags, marc jacobs, supra shoes, soccer shoes, lancel, beats by dre, vans, p90x3, ghd, herve leger, nike free, ferragamo shoes, burberry, birkin bag, juicy couture outlet, montre pas cher, soccer jerseys, ralph lauren, hermes, north face outlet, ugg boots, valentino shoes, louis vuitton, jimmy choo, new balance shoes, mulberry, rolex watches, louis vuitton, abercrombie and fitch, oakley pas cher, reebok outlet, hollister, air max, mont blanc, bottega veneta, converse shoes, ray ban pas cher, karen millen, wedding dresses, hogan, yoga pants, insanity, asics gel, timberland

co wendy said...
This comment has been removed by a blog administrator.
shri hari said...

Thanks for the informative share.
Web Design and Development company in Chennai

Guo Guo said...

yao20150324
michael kors handbags
thomas sabo
valentino shoes
ralph lauren outlet
mac cosmetics
true religion outlet
tods outlet
adidas wings
iphone 6 cases
thomas sabo uk
hollister clothing
ray ban uk
ralph lauren outlet
abercrombie
cheap oakley sunglasses
mont blanc
hollister shirts
burberry outlet online
burberry handbags
replica watches
converse shoes
ghd
tods outlet
toms shoes
lacoste shirts
michael kors outlet online
michael kors handbags
louis vuitton outlet
ferragamo shoes
soccer jerseys
michael kors outlet
ray ban sunglasses

Unknown said...

blake griffin jersey
nike trainers,nike trainers uk,cheap nike trainers,nike shoes uk,cheap nike shoes uk
soccer jerseys
rolex watches,swiss watch,replica watches,rolex watches for sale,replica watches uk,rolex watches replica,rolex watches for sales
moncler jackets
canada goose outlet
nike outlet store
mulberry handbags
raiders jerseys
cheap nhl jerseys
nhl jerseys
nike huarache
peyton manning jersey,eric decker jersey,aqib talib jersey,emmanuel sanders jersey,shannon sharpe jersey,louis vasquez jersey
san francisco 49ers jerseys
nike running shoes
true religion jeans
nike running shoes
north face outlet store
tim tebow jersey,julian edelman jersey,darrelle revis jersey,vince wilfork jersey,tom brady jersey,rob gronkowski jersey,deion branch jersey,jerod mayo jersey,danny woodhead jersey
lebron james shoes,lebron 12,lebron 12 shoes,lebron sneakers,lebron iv,lebron 11,lebron 10
dan marino jersey,louis delmas jersey,brent grimes jersey,cameron wake jersey,mike wallace jersey,dan marino jersey,cameron wake jersey
chelsea football shirts
chicago bears jerseys
air max 2014
hermes outlet store
barcelona jersey
burberry sale
cheap oakley sunglasses
spurs jersey
air jordan shoes
miami heat jersey
coach outlet store
soccer shoes
nike trainers uk
baltimore ravens jerseys
zhuweiming0627

chenlina said...

chenlina201507
pandora charms
true religion
kate spade purses
louis vuitton
oakley sunglasses
christian louboutin sale
michael kors handbags
louis vuitton outlet
chanel outlet online
prada outlet
knockoff watches
concord 11
abercrombie
cheap jerseys
longchamp pas cher
gucci bags
jordan 11 concord
oakley sunglasses outlet
louis vuitton
michael kors outlet online
jordan 11 columbia
louis vuitton
ray ban uk
adidas wings
chanel outlet online
oakley sunglass
mont blanc pen
michael kors outlet
abercrombie
christian louboutin shoes
jordan 11
cheap toms
prada handbags
air max uk
louis vuitton outlet
michael kors outlet
cheap ran bans
ray ban sungalsses
aviator sunglasses
coach outlet store online

Hua Cai said...

abercrombie
babyliss pro
coach outlet online
nike free
oakley sunglasses
evening dresses
michael kors outlet
coach outlet
monster beats
louis vuitton handbags
abercrombie
cheap soccer jerseys
adidas wings
celine outlet
mcm handbags
new balance shoes
fred perry polo shirts
asics shoes
kate spade handbags
tiffany and co
air force 1 shoes
cheap jordans
cleveland cavaliers jersey
new orleans saints jerseys
arizona cardinals jerseys
ugg uk
lebron james shoes
the north face uk
ysl outlet
washington redskins jerseys
adidas shoes
kate spade handbags
cheap toms
hermes belt
ugg boots
prada outlet
nike running shoes
coach outlet store
michael kors canada
cartier love bracelet
2015717caihuali

Zheng junxai5 said...

2015-09-16 zhengjx
Cheap Jordans Shoes For Sale
hollister clothing
Louis Vuitton Handbags Factory Outlet
Gucci UK Outlet Handbags Sale
Christian Louboutin Shoes Outlet Sale Cheap Price
michael kors outlet
Michael Kors Outlet Factory Handbags For Sale
uggs australia
michael kors outlet
michael kors handbags
Cheap Toms Shoes Outlet Sale Clearance
Up to 70% Off Louis Vuitton Outlet
Jordan 6 Retro Discount Sale
louis vuitton
Louis Vuitton Outlet 100% Authentic
burberry outlet
Up To 70% Off Air Retro Jordan 8s
michael kors handbags
michael kors outlet
Coach Factory Outlet Discount Online
Abercrombie And Fitch Clothing Outlet at New York
uggs outlet
Michael Kors Outlet Factory Online Store
Cheap Jordan 13 Shoes For Sale Free Shipping
ralph lauren
canada goose jackets
toms shoes
Abercrombie And Fitch New York Outlet Store
fitflops
coach outlet

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/

mao qiuyun said...

ray ban sunglasses, http://www.raybansunglassesonline.us.com/
nike trainers, http://www.niketrainers.me.uk/
gucci handbags, http://www.guccihandbags-outlet.co.uk/
herve leger, http://www.herveleger.us.com/
timberland boots, http://www.timberlandboots.name/
coach outlet online, http://www.coachoutletonline-store.us.com/
michael kors outlet, http://www.michaelkorsoutletcanada.in.net/
juicy couture outlet, http://www.juicycoutureoutlet.net/
calvin klein underwear, http://www.calvinklein.in.net/
ray ban sunglasses, http://www.raybansunglass.us.com/
prada outlet, http://www.pradaoutlet.us/
lululemon outlet, http://www.lululemonoutletstore.in.net/
tory burch shoes, http://www.toryburchshoesoutlet.com/
michael kors outlet, http://www.michaelkorsoutlet.org.uk/
replica watches, http://www.replicawatchesforsale.us.com/
pandora jewelry, http://www.pandorajewelryoutlet.us.com/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
michael kors outlet, http://www.michaelkorsoutletonlinstore.us.com/
oakley outlet, http://www.oakleyoutlet.in.net/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
michael kors bags, http://www.michaelkorsbags.uk/
ray ban sunglasses, http://www.ray-bansunglasses.eu.com/
oakley sunglasses, http://www.oakley.in.net/
air max 2015, http://www.airmax2015.in.net/
the north face uk, http://www.thenorthfaces.org.uk/
swarovski outlet, http://www.swarovski-outlet.co.uk/
pandora jewelry, http://www.pandora.eu.com/
chanel handbags, http://www.chanelhandbags-outlet.co.uk/
1002maoqiuyun

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

Hua Cai said...

ray ban sunglasses
the north face
ray ban sunglasses
barbour coats
cheap nfl jerseys
cheap uggs
ray ban sunglasses
discount michael kors handbags
the north face outlet store
swarovski crystal
canada goose jackets
tory burch outlet
ugg boots
ray ban sunglasses
nfl jerseys wholesale
ugg boots
louis vuitton outlet
futbol baratas
prada shoes
michael kors outlet
michael kors handbags
michael kors outlet store
lacoste polo shirts
canada goose outlet
michael kors outlet
tory burch shoes
michael kors factory outlet
ugg outlet
oakley sunglasses
coach outlet online
christian louboutin online
toms shoes
jordan shoes
tiffany outlet
designer handbags
prada outlet
prada handbags
louis vuitton handbags
wellensteyn jackets
winter jackets
2015114caihuali

chenlina said...

chenlina20151127
cheap air jordans
mont blanc pens
mulberry handbags
louis vuitton bags
cheap oakley sunglasses
nike huarache
replica watches
michael kors outlet online
toms wedges
louis vuitton handbags
ralph lauren uk
uggs boots for women
ray ban sunglasses outlet
nike air max
celine handbags
hollister co
soccer shoes
michael kors outlet clearance
louis vuitton outlet
uggs on sale
pandora jewelry
fake oakley sunglasses
cheap nfl jerseys
coach factory outlet
oakley sunglasses
jordan concords
coach factory outlet
michael kors handbags
beats by dre
burberry outlet
michael kors bag
celine outlet
louis vuitton outlet stores
michael kors outlet
canada goose jackets
oakley sunglasses cheap
polo ralph lauren
louis vuitton handbags
abercrombie and fitch
coach factory outlet
as

方松腾 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

chenlina said...

chenlina20160127
coach outlet
chanel outlet
celine outlet
hollister shirts
ralph lauren uk
michael kors outlet clearance
adidas originals
michael kors outlet online
abercrombie and fitch
cheap ugg boots
coach outlet online
the north face outlet
mont blanc pens
tiffany and co
abercrombie outlet
coach outlet store
louis vuitton handbags
michael kors handbags
louis vuitton handbags
ray ban sunglasses
tory burch sale
coach outlet
abercrombie outlet
louis vuitton outlet online
ray ban sunglasses outlet
the north face outlet
coach factory outlet online
louis vuitton outlet
michael kors handbags
ralph lauren uk
lebron 13
abercrombie and fitch
louis vuitton outlet
oakley outlet
nike roshe run women
michael kors outlet
louis vuitton handbags
ray bans
canada goose outlet
coach factory outlet
as

Yuanyuan Lin said...

6.29lllllllyuanreplica watches
michael kors handbags outlet
michael kors outlet
fitflops clearance
nba jerseys
nfl jerseys
lululemon outlet online
cheap jordans
michael kors factory outlet
rolex watches
tiffany and co jewelry
toms outlet
michael kors outlet online
prada sneakers
longchamp outlet
burberry outlet online
tory burch outlet
swarovski crystal
gucci sunglasses uk
replica watches
longchamp
michael kors outlet
polo outlet
tiffany jewelry
rolex watches for sale
timberland boots
true religion jeans
mulberry handbags sale
fitflops sale clearance
michael kors handbags
reebok shoes
toms outlet
michael kors handbags
cheap snapbacks
cheap nba jerseys
6.29

John said...

asics gel nimbus
stephen curry shoes
louis vuitton outlet online
vans shoes
cartier watches for sale
tiffany jewelry
designer handbags outlet
hollister clothing store
salvatore ferragamo
louis vuitton outlet online
nike free flyknit 3.0
reebok
coach outlet online
omega watches
lebron james shoes 2016
coach outlet
dior sunglasses
adidas superstars
reebok outlet
coach purses
cheap oakley sunglasses
michael kors bags
kate spade outlet online
designer handbags
coach outlet clearance
longchamp bag
cheap nfl jerseys
valentino shoes
air max 90 black
michael kors outlet online
tiffany jewelry outlet
michael kors outlet clearance
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...

20160923meiqing
louis vuitton outlet
uggs
coach factory outlet
ed hardy
louboutin shoes
ray ban sunglasses
red bottoms shoes
coach factory outlet
polo ralph lauren outlet
ugg outlet

兮兮 said...

20161021 junda
oakley sunglasses
coach outlet canada
uggs
coach outlet
ferragamo shoes
air jordan shoes
canada goose outlet
swarovski crystal
coach outlet
uggs clearance

dong dong23 said...

replica rolex watches
uggs uk
denver broncos jerseys
michael kors handbags
moncler pas cher
michael kors canada
cheap ray ban sunglasses
louis vuitton outlet
celine outlet
ugg boots
louis vuitton handbags
coach outlet store online
cincinnati bengals jerseys
moncler jackets
michael kors handbags
coach outlet
michael kors handbags
louis vuitton
ugg canada
ugg boots
true religion jeans
louis vuitton purses
louis vuitton
mont blanc pens
adidas trainers
louis vuitton outlet
louis vuitton outlet
cheap ugg boots
ray ban wayfarer
christian louboutin shoes
coach outlet
ugg boots
christian louboutin sale
ugg boots
hollister clothing
michael kors outlet
adidas yeezy
ralph lauren outlet
fitflops
minnesota vikings jerseys
201611.5wengdongdong

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

Hua Cai said...

cheap nba jerseys
ray bans
coach factory outlet
true religion outlet
nike huarache
cheap oakley sunglasses
fitflops
oakley sunglasses
instyler
coach outlet
20170114caihuali

raybanoutlet001 said...

omega watches for sale
michael kors outlet
asics shoes
michael kors handbags clearance
cleveland cavaliers jersey
adidas nmd r1
michael kors handbags
ralph lauren
skechers outlet
los angeles lakers 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...

adidas nmd
michael kors handbags outlet
nike trainers uk
michael kors
atlanta falcons jersey
detroit lions jerseys
reebok shoes
miami heat jersey
michael kors outlet clearance
replica rolex