How to get the original URI extension using PHP Tonic?

252 Views Asked by At

I'm developing a REST API using PHP and Tonic. The framework has a fine feature which is the inference of the Content-Type based on the extension of the URI requested. In the case of the service I'm developing, it's important to know the full name of the requested file.

If I use the following code:

$this->request->uri;

For a URI such as http://service.com/image1.jpg, all I get is image1.

I could get the information I need going straight through $_SERVER['REQUEST_URI'], but I'd like to use the facilities of the framework for this (should there be any). Also, I found the documentation pretty poor, so, any doc links will be appretiated as well. Thanks.

1

There are 1 best solutions below

3
On

You should take a look here:

private function getURIFromEnvironment($options)
{
    $uri = $this->getOption($options, 'uri');
    if (!$uri) { // use given URI in config options
        if (isset($_SERVER['REDIRECT_URL']) && isset($_SERVER['SCRIPT_NAME'])) { // use redirection URL from Apache environment
            $dirname = dirname($_SERVER['SCRIPT_NAME']);
            $uri = substr($_SERVER['REDIRECT_URL'], strlen($dirname == DIRECTORY_SEPARATOR ? '' : $dirname));
        } elseif (isset($_SERVER['REQUEST_URI'])) { // use request URI from environment
            $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
        } elseif (isset($_SERVER['PHP_SELF']) && isset($_SERVER['SCRIPT_NAME'])) { // use PHP_SELF from Apache environment
            $uri = substr($_SERVER['PHP_SELF'], strlen($_SERVER['SCRIPT_NAME']));
        } else { // fail
            throw new \Exception('URI not provided');
        }
    }

Specifically:

    $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

I would just dump $_SERVER and see whats available to you and replace or add with your need.