play_ Posted October 1, 2010 Share Posted October 1, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214938-still-struggling-to-make-url-routing-the-way-i-want-it-to/ Share on other sites More sharing options...
roopurt18 Posted October 1, 2010 Share Posted October 1, 2010 Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/214938-still-struggling-to-make-url-routing-the-way-i-want-it-to/#findComment-1118103 Share on other sites More sharing options...
play_ Posted October 1, 2010 Author Share Posted October 1, 2010 Quote Quote 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 =( Quote Link to comment https://forums.phpfreaks.com/topic/214938-still-struggling-to-make-url-routing-the-way-i-want-it-to/#findComment-1118112 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.