2

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.

Muc
  • 1,464
  • 2
  • 13
  • 31

1 Answers1

2

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.

Matthew Camp
  • 866
  • 5
  • 9
  • Thanks, I'll try that, but I was trying to come up with a way of getting this information without changing the framework's code. – Muc Aug 12 '13 at 14:54
  • Yeah, from what I could tell(Which I didn't look at it too long) there wasn't anything built in for that. – Matthew Camp Aug 12 '13 at 14:58
  • very few people use tonic, have problem thinq you can help? http://stackoverflow.com/questions/34457064/php-tonic-not-working-as-expected-with-routes – Lucas Dec 27 '15 at 20:07