How do you manage your url in php application ? (for CodeIgnitor guys)

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” 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”] = ““;
$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 🙂

read the rest of the thread here
you can find out solution here

best wishes,

work for fun!


3 responses to “How do you manage your url in php application ? (for CodeIgnitor guys)

  1. agree with you, in every project we should follow this, on my next project i will implement it, and will knock you if you get any problem. 🙂

  2. My sister and I happen to be just debating this very topic, he’s usually looking to prove me wrong. Your take on it is perfect and exactly how I really feel. I just now e mailed my cousin this blog to demonstrate him your perspective.

  3. whoah this weblog is magnificent i love reading your
    articles. Keep up the great work! You understand, many persons are hunting
    round for this info, you can aid them greatly.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

my tweets

May 2009
« Apr   Jul »

Flickr Photos

RSS my shared links on google reader

  • An error has occurred; the feed is probably down. Try again later.
%d bloggers like this: