spaze Posted June 16, 2009 Share Posted June 16, 2009 Hello all, I am writing my own framework and stumbled upon a problem. I want to determine controller and action from the given URL. So far I have been using the URI to fetch the first and second section from the URI, but this is limited to having the application and domain root (www.domain.com) If I would have my framework in sub-directories, ie. www.domain.com/sub1/sub2/sub3, and then call index/show, the first section I would fetch would be sub1 and second sub2 How can I effectively code this so that I would always get the index and show from www.domain.com/sub1/sub2/sub3/index/show even if I would have more sections after this? What I'm tryng to say is, how can I dynamically fetch these two like so: www.domain.com/index/show -> URI = index/show www.domain.com/sub1/sub2/sub3/index/show/param1/param2 -> URI = index/show Currently the latter returns sub1/sub2/sub3/index/show/param1/param2 and this is not what I need. Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/ Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 You need to add an option to set the base uri within your controller. By default this would be set to / but if I wanted to use your framework from within the subdirectory foo, I would set the base uri to be /foo. Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857018 Share on other sites More sharing options...
spaze Posted June 16, 2009 Author Share Posted June 16, 2009 You need to add an option to set the base uri within your controller. By default this would be set to / but if I wanted to use your framework from within the subdirectory foo, I would set the base uri to be /foo. Can I do this from the PHP or do I have to modify the .htaccess file? Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857043 Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 You would write it into your framework. Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857047 Share on other sites More sharing options...
spaze Posted June 16, 2009 Author Share Posted June 16, 2009 You would write it into your framework. I do have BASE_DIR parameter that only shows where the controllers are located, but this won't help me get the requested controller name from the URL. Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857048 Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 Of course it won't, it has nothing to do with what you are asking. Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857050 Share on other sites More sharing options...
spaze Posted June 16, 2009 Author Share Posted June 16, 2009 Of course it won't, it has nothing to do with what you are asking. Since I don't quite get what you trying to say, can you lead me a little toward the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857057 Share on other sites More sharing options...
trq Posted June 16, 2009 Share Posted June 16, 2009 I'm not sure what you approach is for getting your controllers / actions at present but I'll assume it's something simple. A simple example. function parseuri($uri, $basepath=null) { $uri = str_replace(array('http://', $_SERVER['SERVER_NAME'].'/'),'',$uri); if (null != $basepath) { $uri = str_replace($basepath,'',$uri); } $parts = explode('/', $ur); return array('controller' => $parts[0], 'action' => $parts[1]); } print_r(parseuri('http://foo.com/c/a')); print_r(parseuri('http://foo.com/subdir/c/a','subdir')); This is just an example, and not at all tested but I hope it gets the idea accross. Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857072 Share on other sites More sharing options...
spaze Posted June 16, 2009 Author Share Posted June 16, 2009 I'm not sure what you approach is for getting your controllers / actions at present but I'll assume it's something simple. A simple example. function parseuri($uri, $basepath=null) { $uri = str_replace(array('http://', $_SERVER['SERVER_NAME'].'/'),'',$uri); if (null != $basepath) { $uri = str_replace($basepath,'',$uri); } $parts = explode('/', $ur); return array('controller' => $parts[0], 'action' => $parts[1]); } print_r(parseuri('http://foo.com/c/a')); print_r(parseuri('http://foo.com/subdir/c/a','subdir')); This is just an example, and not at all tested but I hope it gets the idea accross. Thanks for your reply. I have done this already and current version does just this, but since the user needs to configure the application by having to know the base I was hoping to find a way to automize this. It would be neat to find the base_uri from which the script is running so that I could replace the URI portion of this base_uri with blank. Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857214 Share on other sites More sharing options...
trq Posted June 17, 2009 Share Posted June 17, 2009 That would be more problematic than helpful. What if your users are using aliased directories? Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857786 Share on other sites More sharing options...
spaze Posted June 17, 2009 Author Share Posted June 17, 2009 That would be more problematic than helpful. What if your users are using aliased directories? I see your point Perhaps the configuration of BASE_URL is not so complicated after all. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/162372-creating-my-own-framework-have-a-question-about-uri/#findComment-857801 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.