Jump to content

Creating my own framework, have a question about URI


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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