Jump to content

Creating my own framework, have a question about URI


spaze

Recommended Posts

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.

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?

 

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.