Tuesday, August 27, 2013

Routing URI With Forward Appended Slashes in ZF2

You may sometimes want to route forward appended URIs to its last corresponding action.

For example:

http://www.sample.com/mycontroller/login/ (WILL NOT route to login action)
http://www.sample.com/mycontroller/login (WILL ROUTE to login action)
http://www.sample.com/mycontroller/ (WILL NOT ROUTE to index action)
http://www.sample.com/mycontroller (WILL ROUTE to index action)

And assume this Route setting for above:

array(
    'mycontroller' => array(
        'type'    => 'Segment',
        'options' => array(
            'route'    => '/mycontroller',
            'defaults' => array(
                'controller' => 'SomeModule\Controller\MyController',
                'action'     => 'index',
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'login' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/login',
                    'defaults' => array(
                        'controller' => 'SomeModule\Controller\MyController',
                        'action'     => 'login',
                    ),
                ),
                'may_terminate' => true,
            ),
        ),
    ),
);

To make the forward appended slashes default to the last action in the URI:
array(
    'mycontroller' => array(
        'type'    => 'Segment',
        'options' => array(
            'route'    => '/mycontroller',
            'defaults' => array(
                'controller' => 'SomeModule\Controller\MyController',
                'action'     => 'index',
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'default' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/[:action][/]',
                    'constraints' => array(
                        'action'     => '[a-zA-Z][a-zA-Z0-9_-]+',
                    ),
                    'defaults' => array(
                        'controller'    => 'SomeModule\Controller\MyController',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
            ),
            'login' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/login[/]',
                    'defaults' => array(
                        'controller' => 'SomeModule\Controller\MyController',
                        'action'     => 'login',
                    ),
                ),
                'may_terminate' => true,
            ),
        ),
    ),
);

Notice that I added a "default" child route so that I will not have to worry if a I added a new action and forget to create a route for it. Also notice the "[/]" which means that forward slashes are optional now and that's basically it. You just have to add "[/]" in each route of a Segment type.

http://www.sample.com/mycontroller/login/ (WILL ROUTE to login action)
http://www.sample.com/mycontroller/login (WILL ROUTE to login action)
http://www.sample.com/mycontroller/ (WILL ROUTE to index action)
http://www.sample.com/mycontroller (WILL ROUTE to index action)

If you dont want a default fallback route, you can do just so.
array(
    'mycontroller' => array(
        'type'    => 'Segment',
        'options' => array(
            'route'    => '/mycontroller[/]',
            'defaults' => array(
                'controller' => 'SomeModule\Controller\MyController',
                'action'     => 'index',
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'login' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/login[/]',
                    'defaults' => array(
                        'controller' => 'SomeModule\Controller\MyController',
                        'action'     => 'login',
                    ),
                ),
                'may_terminate' => true,
            ),
        ),
    ),
);
Notice that I added "[/]" in the parent route because we removed the default child fallback route.
Now your route with forward slashes should work.

20 comments:

Jayawi said...

Thanks for this post. I was having trouble with slashes and child routes. Leaving the trailing slash optional in the parent led to the child route not being found and omitting it lead to the parent route only being recognisable without it.
Using a 'default' child route solved the issue though for my purposes, I only left the slash and omitted the action.

Anonymous said...

Thavisha Lakindu Peiris: His parents said he was
courier insurance convinced there would be no scope for confusions anywhere.


My website; courier car rental insurance ()

Anonymous said...

You're so interesting! I don't think I have read through a
single thing like that before. So nice to discover another person with
some genuine thoughts on this subject. Seriously..
thank you for starting this up. This site is something that is required on the internet, someone with
some originality!

Here is my page :: الكشف عن تسرب المياه

Anonymous said...

Hmm it appears like your website ate my first comment (it
was extremely long) so I guess I'll just sum it up what I submitted and say,
I'm thoroughly enjoying your blog. I too am an aspiring
blog blogger but I'm still new to everything. Do you have any tips for beginner blog
writers? I'd genuinely appreciate it.

Visit my site - Kritika Chaos Unleashed Hack

oakleyses 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

Anonymous said...

A social affair that as well sells condition or motorcar
policy, bring up to maintain in persuasion following reading
you need to fille out on a ambulatory commerce for you.
If a donor without consent or permission. If you poorness to acquisition yellow jewelry to a eating place for
a small f- Cheap Jordans Cheap Jordan Shoes (cottagecemetery.org) Cheap Jordans For Sale Cheap Jordans (www.softroom.co.kr) Cheap Jordans Cheap Jordans Shoes (); , Cheap
Jordan Shoes (http://202.123.179.229/lao-ntfpwiki/index.php/Cheap_Jordan_Shoes_forums_are_of_import_slipway_to_get_enclosed_in_is_recommended) Cheap Jordans Cheap Jordan For Sale (rhcloud.com) Cheap Jordans For Sale Cheap Jordan Shoes Cheap Jordans Cheap Jordans Cheap Jordan Cheap Jordans Cheap Jordans Shoes (http://sousaesantosadvocacia.com.br/index.php?title=Cheap_Jordan_Shoes_be_a_consumer_goods_transport_all_the_unsurpassable_affair_to) you to draw
from your cover. This can check that you want to immersion on your be, so take a
leak reliable to use material commerce is an biogenic
commerce instrument among sure audiences but hold open in watch the advice
mentioned in this determiner. make up one's mind why you need needto

Unknown said...

Sergei , October 22, 1970 Born in Christian Louboutin Bois Dore Moscow State Ozherelye. Perhaps influenced by Cheap LV Handbags their parents worship warrior army, join the army in cheap jordans 1989, and into the elite airborne military service. 1991, by the influence of the Soviet Union, the uggs on sale Russian christian louboutin army general lack of funds, a large number ugg australia of Air Jordan 11 Gamma Blue soldiers seeking veterans, but under extremely difficult conditions still dedicated, praise superiors, and soon was sent to the famous christian louboutin shoes Ryazan Higher ugg Airborne Command School studies. After graduating in 1994, entered the famous Pskov 76th Guards Airborne discount christian louboutin Division, served as discount nike jordans the reconnaissance platoon, reconnaissance ugg soldes deputy company commander, company commander, battalion airborne regiment scouting director and other duties.From 2000 to Cheap Louis Vuitton Handbags 2004, where the forces have repeatedly ordered war with Chechen militants. Because of their opponents in order to form the squad activity and haunted impermanence, the Russians had to ugg boots mobilize elite troops set up large uggs outlet number of small units, "a Discount Louis Vuitton small wholesale jordan shoes play small" Implementation siege. commanding troops in Chechnya, Christian Louboutin Daffodile Ingushetia and other places over the mountains, to track the militants fled. Discount LV Handbags Although the militants were extremely vigilant, but as long as the task has never had outsmarted.August 8, 2008, Georgia suddenly invade South Ossetia (Russian peacekeepers stationed ugg pas cher there), the Russian military to respond quickly, including including the 76th Guards Airborne Division, more than christian louboutin remise 50% 3,000 people were immediately delivered to the pro-Russian Abkhazia, Georgia cheap nike jordan shoes Army contain two infantry brigades, effectively coordinate the cheap christian louboutin direction of the Russian Bags Louis Vuitton troops in South Ossetia.

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

Unknown said...

2015-09-16 zhengjx
nike trainers
toms outlet
celine handbags
michael kors outlet
Hollister UK Clothing Store
Louis Vuitton Outlet Discount Handbags
Cheap Louis Vuitton Handbags Luggage Bags Sale
cheap jordans
hollister uk
michael kors bags
ugg boots sale
Gucci Outlet Online 80% OFF
ugg australia
michael kors outlet
Mont Blanc Pens For Sale
Michael Kors Outlet Online 70% Off Handbags
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

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

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

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

Unknown said...

coach outlet
hollister
cheap oakley sunglasses
rolex watches
nike foamposite
ray ban sunglasses
longchamp handbags
cartier watches for sale
ferragamo shoes
coach outlet
20170114caihuali

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

pandora jewelry
adidas superstar
cheap jordans
adidas shoes
hogan outlet online
led shoes
adidas tubular
nike air zoom
led shoes
longchamp outlet

Unknown said...

ralph lauren polo
ugg outlet
cheap nfl jerseys
michael kors handbags outlet
nike shoes
jacksonville jaguars jersey
ferragamo outlet
rolex replica watches
polo ralph lauren
nike blazer

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