Jump to content

Still struggling to make URL routing the way i want it to


play_
 Share

Recommended Posts

Creating a basic MVC framework.

I may confuse myself while asking this, so bare with me if this is confusing for you also...

 

 

As far as i understand, the URLs are in the following format:

www.example.com/controller/method/args

 

My goal here is to not show 'method' in the URL if it's not necessary.

 

For example,

www.example.com/contact

 

instead of

www.example.com/contact/view.

 

This is OK. I can accomplish this. But problems arise when I want to pass arguments in the URL.

 

Say I have the URL:

www.example.com/users  <----- this would by default, user the controller 'users' and call default method, 'view'..just list all users.

 

But say i want to view a user.. John.

www.example.com/users/john

 

How can I make my script know that 'john' is an argument, and not a method? The only way I can think of this is if I always have a method in the url .. like www.example.com/users/view/username/john

 

As of now, my script breaks up the URL at / and the first element in the array is is the controller. It calls the controller with the other arguments.

 

so www.example.com/users/john. The script would do (simplified)

 

$controller = 'users'

 

$controller = new $controller('john')

 

the 'Users' class would then see if 'john' is a method. If it is, call it, if not, then call the default method (view) with john as argument...

but what if I have a method called 'edit' and someone's username is edit? then www.example.com/users/edit would be a problem.

 

So my guestion to you guy is... how do you deal with this issue?

 

If you have www.example.com/users/edit, how do you let the script know that 'edit' is someone's username and now a method to call?

Do you pass the method in the url like so www.example.com/users/edit/edit ?

 

Or if you have worked with a framework (Zend, cakePHP, Symfony, CodeIgniter, etc), how does the framework handle it?

 

Ideally, i would like to be able to have www.example.com/users/john instead of www.example.com/users/view/id/john

Link to comment
Share on other sites

Ideally, i would like to be able to have www.example.com/users/john instead of www.example.com/users/view/id/john

I really don't see what's wrong with the latter there nor any real way around it.

 

It would be easier for people to remember.

I also think shorter is neater...but if there's no way around it...then there isn't. i've sat and tried to think this out for ~2hrs =(

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.