Author Topic: Creating my own framework, have a question about URI  (Read 1260 times)

0 Members and 1 Guest are viewing this topic.

Offline spazeTopic starter

  • Irregular
  • Posts: 29
    • View Profile
Creating my own framework, have a question about URI
« on: June 16, 2009, 07:18:04 AM »
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.

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #1 on: June 16, 2009, 07:25:37 AM »
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.

Offline spazeTopic starter

  • Irregular
  • Posts: 29
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #2 on: June 16, 2009, 07:56:45 AM »
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?

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #3 on: June 16, 2009, 07:59:44 AM »
You would write it into your framework.

Offline spazeTopic starter

  • Irregular
  • Posts: 29
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #4 on: June 16, 2009, 08:02:33 AM »
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.

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #5 on: June 16, 2009, 08:06:37 AM »
Of course it won't, it has nothing to do with what you are asking.

Offline spazeTopic starter

  • Irregular
  • Posts: 29
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #6 on: June 16, 2009, 08:13:55 AM »
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?

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #7 on: June 16, 2009, 08:41:20 AM »
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.
« Last Edit: June 16, 2009, 08:42:12 AM by thorpe »

Offline spazeTopic starter

  • Irregular
  • Posts: 29
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #8 on: June 16, 2009, 11:31:51 AM »
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.

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #9 on: June 17, 2009, 03:38:14 AM »
That would be more problematic than helpful. What if your users are using aliased directories?

Offline spazeTopic starter

  • Irregular
  • Posts: 29
    • View Profile
Re: Creating my own framework, have a question about URI
« Reply #10 on: June 17, 2009, 04:32:05 AM »
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!