Advanced routing for component URLs

594 views
Skip to first unread message

Jon Lackey @zuno

unread,
Feb 25, 2009, 5:05:43 AM2/25/09
to Joomla! General Development
Hey all,
I'm looking for a good reference, resource or tutorial for routing
URLs for a custom component. I've got the basic router.php file
working, but I want to get a bit more crafty :)

For instance my current routing generates urls like this...
www.domain.com/view/id

I want to do something like this...
www.domain.com/title-of-item-from-custom-component

Any help is greatly appreciated.
-Jon

Schalk Versteeg

unread,
Feb 25, 2009, 5:19:50 AM2/25/09
to joomla-de...@googlegroups.com
Hi Jon,
What would be easier to achieve is URL looking like this...

Schalk

Jonathan Lackey @zuno

unread,
Feb 25, 2009, 5:24:26 AM2/25/09
to joomla-de...@googlegroups.com
Yeah, I just got that working, but my issue is that these items are deep into a menu system, so I kinda lied about how my urls really look. They actually look like this....


Domain / Parent menu item / child menu item / component view / id

So I've got a parent menu item, child menu item then my view then my id. I'm trying to generate the URLs without redoing my menu structure and without including the view.

I feel I'm really close to figuring it out.

Thanks for the help!


Jonathan Lackey | vp, creative director
509 N Elena Ave, #2 . Redondo Beach CA . 90277
t:424.247.8616 . c:310.698.9330 . aim: webmonkiee
zunostudios.com

Ian MacLennan

unread,
Feb 25, 2009, 11:38:48 AM2/25/09
to joomla-de...@googlegroups.com
There should be no real need to include a view. You just have to
encode your urls in your buildroute function such that you can figure
out what type of view it is based on that url. There are many ways to
do this. You could do this by looking at the number of segments and
inferring the view based on that. For example, taking the com_content
component, you could build URLs such that:
an article view is always shown as section/category/article_id, for a
total of three segments. Therefore, if you get three segments passed
to your parse function, you know that view = article. A category view
could always be shown as section/category, for a total of two
segments. Therefore, if you get two segments passed to your parse
function, you know that view = category. A section view could always
be shown as section, for a total of one segment. Therefore, if you
get one segment passed to your parse function, you know that view =
section.

This is one way to do it, but if course there are others. I believe
you can put mixed case in your URLs, so you could make certain things
case sensitive and base it on that. You could come up with some other
clever algorithm as well.

You basically have free reign as to how you build and parse your
routes. The only real requirement for a properly functioning system
is that:
parseRoute(buildRoute($query)) == $query

Hope that helps,
Ian


On Wed, Feb 25, 2009 at 5:24 AM, Jonathan Lackey @zuno
<webmo...@gmail.com> wrote:
> Yeah, I just got that working, but my issue is that these items are deep
> into a menu system, so I kinda lied about how my urls really look. They
> actually look like this....
> www.domain.com/work/websites/samples/19
> Domain / Parent menu item / child menu item / component view / id
> So I've got a parent menu item, child menu item then my view then my id. I'm
> trying to generate the URLs without redoing my menu structure and without
> including the view.
> I feel I'm really close to figuring it out.
> Thanks for the help!
>

Rob Schley

unread,
Feb 26, 2009, 4:22:48 AM2/26/09
to joomla-de...@googlegroups.com
Ian's advice is pretty good. The routing system is pretty simple if you are doing simple things but it can get quite complex when you want to do sophisticated things. I've written a router that was only ~50 lines of code but I've also written a router that was over 600 lines of code.

Counting segments is usually a good way to go but you can also create "reserved" words in your extensions that influence the way the URL is built. For example, with one of the extensions I am working on to submit an item to a category the route is: /category-title/submit.html and "submit" becomes a "reserved" word. Based on the number of segments and/or the value of certain segments you can do a lot of interesting things. I've been trying to get away from using numeric ids in URLs and so I require and enforce uniqueness of the alias and then look up the alias when parsing the route. There is a slight performance ding for doing that but I think the result is worth it.

Best,
Rob

Jonathan Lackey @zuno

unread,
Feb 26, 2009, 4:27:20 AM2/26/09
to joomla-de...@googlegroups.com
Thanks Rob and Ian. Great help as always. I'll give these concepts a try.

-Jon


Jonathan Lackey | vp, creative director
509 N Elena Ave, #2 . Redondo Beach CA . 90277
t:424.247.8616 . c:310.698.9330 . aim: webmonkiee
zunostudios.com
Reply all
Reply to author
Forward
0 new messages