SCRIPT_NAME is available via server params already:
$scriptName = $request->getServerParams()['SCRIPT_NAME'];
However, SCRIPT_NAME is the path on the filesystem to the script. This
is often used to try and determine if the path contains a portion of
the script name in it, in which case that segment will be stripped
from the path for purposes of routing.
All of this could be done via a router, which will likely also be what
is used to assemble URI links for your application. The question is:
should that functionality be pushed into the ServerRequest, or left to
the consumer?
If we decide to make this a responsibility of the ServerRequest, then
the next question is: what do we call it? In ZF, we've called this the
"base path". tob2bot referenced "PATH_INFO", but within the SAPI,
that's the path present in the incoming request URI. My take is that,
since we're talking about a prefix in the path that we want to strip
off or resolve to, "BASE_PATH" is likely what we want, which would
suggest the addition of these two methods:
withBasePath($path) : ServerRequest
getBasePath() : string
My question is: does this really need to be in the interface?
When I look at various implementations, the base path is typically
only of interest for:
- routing
- URI assembly
Typically, the latter is a function of the router (as you want to
assemble a URI based on configured routes). When I look at ZF2, we
have exactly two places where the base path is consumed:
- our router — which stores the value, and uses it for route generation.
- our "base path" view helper (which gets the value on instantiation)
I could see having a service or utility method that is passed the
Request instance, and derives the value to pass to either of these,
and/or making that a function of the router. Considering all the
values necessary for deriving the value are already present in the
Request, adding methods around this would, at best, be a convenience,
and could be interpreted as being outside the scope of the Request. In
talking to Larry, it looks like the situation in Symfony is similar.
That said... the methods exist on the current Request implementations,
which makes a case for adding them to PSR-7. I could take it or leave
it at this point; I need to know if this is a show-stopper for any of
you.
Thoughts?