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

19 comments:

Unknown said...
This comment has been removed by a blog administrator.
Unknown said...

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

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/

Unknown 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

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

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

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

caiyan said...

gucci
coach outlet store online
longchamp bags
michael kors bags
pandora jewellery
patriots jerseys
yeezy boost
longchamp handbags
adidas superstar shoes
eltics jerseys
0413shizhong

Unknown said...

snapbacks wholesale
gucci outlet online
coach outlet online
cheap oakley sunglasses
michael kors outlet online
ralph lauren outlet
michael kors outlet clearance
pandora jewellery
cheap ray ban sunglasses
michael kors outlet clearance
20170617lck

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...

yeezy shoes
longchamp bags
yeezy boost 350
adidas nmd
hermes belt
brady jersey
michael kors outlet
longchamp bags
adidas tubular shadow
chrome hearts

Unknown said...

nhl jerseys
michael kors outlet
cheap ray ban sunglasses
versace jeans
michael kors handbags
prada outlet
ugg boots
ugg outlet
kate spade sale
michael kors uk

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

John said...

yeezy shoes
adidas nmd runner
rangers jerseys
jimmy choo shoes
pandora jewelry
tiffany and co outlet
longchamp sale
purs jerseys
fit flops
air max shoes
20170817yuanyuan

Unknown said...

20180412xiaoke
ugg boots
kobe 9 elite
oakley sunglasses
adidas outlet
cheap nhl jerseys
oakley sunglasses sale
michael kors outlet
polo ralph lauren
prada shoes for men
nike shoes

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...

qzz0710
ray ban sunglasses
air jordan 3
stuart weitzman shoes
tory burch outlet
fitflops sale
kd 10 elite
bulls jerseys
ralph lauren polo shirts
ugg outlets
burberry outlet

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

water treatment expert said...

Water Treatment Expert Inc |Water Treatment Plant Delhi


MECHANICAL WORKS. Wtexpert is having state of the art mechanical fabrication facility at dedicated sub. FACILITIES. Above units spread in areas of 8000 ...

http://wtexpert.com/
http://wtexpert.com/effluent-treatment-plant/
http://wtexpert.com/sewage-treatment-plant/
http://wtexpert.com/ultra-filtration-system/
http://wtexpert.com/reverse-osmosis-plant/
http://wtexpert.com/filtration-systems/