Recently i had a post in phpexperts group where i wanted to emphasis on managed url. hard coding url is not a good practice besides it ended up with lot of changes on tested code.
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
-> 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
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))
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 🙂
work for fun!