Respect/Rest

Autodetect Path

Closed this issue · 2 comments

Hi guys,

Came across something today when moving between different servers. Currently you must declare the script path as such:

    $router = new Router("/blindsamson/dev/rest");

I threw something together and was wondering if it's make sense to make this easier to do out of the box.

This was my first implementation:

    $script_path = explode("/", $_SERVER["SCRIPT_NAME"]);
    unset($script_path[count($script_path) - 1]);
    $script_path = implode("/", $script_path);

    $router = new Router($script_path);

And to make it a one-liner I went with:

    $script_path = str_replace($_SERVER["DOCUMENT_ROOT"], "", dirname(__FILE__));

Thoughts? Or is there a better way to do this?

@blindsamson what Router takes AFAIK is the URI or endpoint for this particular service.

i.o.w. the part coming after http://your.host.name

What you get from __FILE__ or $_SERVER["DOCUMENT_ROOT"] is the filesystem paths

ex. /val/www/something.php

These do not have to be comparable with each other.

You can route many URI paths to the same script. If I understand what you are asking that is... please advice.

One can always instantiate with Respect\Rest\Router($_SERVER["DOCUMENT_ROOT"]) as well and there are times when the document root is not what one expects to be interpreted to be actually what the routes are going to be resolved against.