Search the web
Sign In
New User? Sign Up
phpexperts
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want to share photos of your group with the world? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
How you manage your url in php application ? (for CodeIgnitor guys)   Message List  
Reply | Forward Message #12057 of 13296 |
Hi,
It has been long while since i last post here in phpexperts group, well i was
away from php for pretty long, being more java and ruby on rails guy when i got
chance to help few php projects, i borrowed few more neat and nifty design
concept from rails to php.

well let me tell you about the story of stories, i ain't treat myself as php
expert anymore since being detached or keeping my ass out of it. i'd prefer to
call myself as the expert of "work for fun".

before digging into the details let me explain what was wrong and what could be
wrong with your current way of using URL through out the php application (web
site)

-> Karim, he is given a task to develop a beautiful WOW WOW application in php,

-> He is very WOW WOW developer, develops everything on the fly, produces
(TR/Z/B)*illion of bugs on the fly as well.

-> Very promonient developer, he knows how to write php along html, after
hearing several good advices he started giving CodeIgnitor a shot.

-> So he used to write the following kinda code in everywhere -

<a href="<?= site_url("user/profile/10") ?>">My profile</a>

Now if you get a chance to look into his view or controller codes, you might see
in everywhere he hard coded the url pattern.

what the hell is the URL Pattern?

well, you see "site_url("user/profile/10")" this code is expanded to
"http://abc.com/user/profile/10" while you execute your code in php. this
"user/profile/:number" is called url pattern.

everywhere in his code base he kept such hard coded url pattern.
let's imagine his client or boss or team lead ask him to change the url pattern
"user/profile/:number" to something similar "profile/:number/".

now tell me what would YOU DO? if you were placed in such situation?

well you know if that guy was me, i'd run a string replace command through out
the whole project. so wherever i wrote "user/profile/:number" url pattern that
would be converted to "profile/:number"

something like this - /site_url\("user\/profile\/(\d+)"\)/ replace to
site_url("profile/$1")


well i guess many of you already have such problem with similar solution ;)
frankly speaking we had similar problem thus we came up with some solution where
we can change URL pattern without modifying existing view or controller code.

here is the evidence -
<a href="<?= $this->url->profile(array("username" => $user_profile->username))
?>">
<img src="<?= $this->url->avatar(array("username" => $user_profile->username))
?>" />
</a>

you see, we ain't hard coding any url anymore, rather we are calling a function
which are automatically generating from the following kinda configuration -
$urls["profile"] = "http://:username.:host/community";
$urls["avatar"] = "http://avatar.somewherein.net/avatar/:username/for/aawaj";
$urls["logout"] = "logout";

i am pretty sure you guys are smart enough to figure out how we did that, let me
know if you need any help about how we did that.

well first you try yourself and tell me how we did :)

best wishes,
nhm tanveer hossain khan (hasan)
http://hasan.we4tech.com
twit @we4tech
work for fun!




Mon May 18, 2009 7:09 pm

hasan83bd
Offline Offline
Send Email Send Email

Forward
Message #12057 of 13296 |
Expand Messages Author Sort by Date

Hi, It has been long while since i last post here in phpexperts group, well i was away from php for pretty long, being more java and ruby on rails guy when i...
hasan83bd
Offline Send Email
May 18, 2009
7:16 pm

Great post hasan vai. It might be your blog post also :P ... Best regard Tareq Hasan My Blog (http://tareq.weDevs.com) Developers forum:...
Tareq Hasan
tareq.hasan
Offline Send Email
May 18, 2009
7:29 pm

Dear Hasan Vai, I have a question for you regarding the url issue for codeigniter. I got your point of ease of managing urls. The question is regarding...
Himel Nag Rana
h_n_rana
Online Now Send Email
May 19, 2009
6:03 am

hi hasan, i don't know how you guys exactly did this. but i can see the basic idea. i would like to share my 2 cents about your implementation (without seeing...
Anupom
ae_sumerians98
Offline Send Email
May 19, 2009
6:50 am

and this is how I would implement this (without the caching though), class Url { private $urls = array( "profile" => "http://<username>.<host>/community", ...
Anupom
ae_sumerians98
Offline Send Email
May 19, 2009
7:28 am

Hi anupom bhai, you are absolutely right about it, just here is the complete one with same request cache and keeping urls mapping in separate configuration...
hasan83bd
Offline Send Email
May 19, 2009
5:34 pm

Hi, get my inline comments - ... you know url get changed or you get better fluent pattern to keep in url so you need to change them. better design it keeping...
hasan83bd
Offline Send Email
May 19, 2009
6:03 pm

Hi Rana bhai, actually this approach is used to adopt alternative approach of not hard coding any url in php file. so we could change the url pattern without...
hasan83bd
Offline Send Email
May 19, 2009
6:04 pm

Dear hasan vai, I got your point regarding performance. That sounds good. For URL pattern recognition we are currently using routes.php configs and with slight...
Himel Nag Rana
h_n_rana
Online Now Send Email
May 19, 2009
6:49 pm

sorry for the late reply. thanks a lot Hasan for raising this up, it's ofcourse a very handy and life saving feature. change is the rule of the web universe...
Anupom
ae_sumerians98
Offline Send Email
May 22, 2009
5:29 pm

Dear Hasan vai, Thanks a lot for your great post. URL patterns surely can help us for building dynamic, hackable and SEO friendly URLs. BTW, URL pattern is...
Anis Ahmad
anis_niit
Offline Send Email
May 19, 2009
10:29 am

... $router = $ctrl->getRouter(); $router->addRoute( 'user', new Zend_Controller_Router_Route('user/:username', array('controller' => 'user', 'action' =>...
hasan83bd
Offline Send Email
May 19, 2009
6:17 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help