I have a self-built MVC fw with a router routing URLs such that the common example.com/controller/action is used. I’m running into issues when my application is deployed within a sub-directory such as
example.com/my_app/controller/action/?var=value
My router thinks my_app is the name of the controller now and controller is the method.
My current solution is to manually ask for any sub directory name in a config at install. I’d like to do this manually. See my question below and let me know if I’m going about solving this the wrong way and asking the wrong question.
My question:
if I have two paths, how do I truncate the common pieces from the end of one and remove it from the end of the other.
A = /var/www/my_app/pub
B = /my_app/pub/cntrl/actn
What’s your quickest one liner to remove /my_app/pub from B and remain with /cntrl/actn?
Basically looking for a perl-esque way of getting the common denominator like string.
Thanks for any input
The way I would solve this is adding this logic to the front controller (the file to which your server sends all nonexistant file requests, usually index.php).
Here’s a codepad demo.
What does this do? If the front controller is located (relative to the www root) in:
/subdir/myapp/, then it’s$_SERVER['SCRIPT_NAME']would be/subdir/myapp/index.php. The actual request URI is contained in$_SERVER['REQUEST_URI']. Let’s say, for example, that it is/subdir/myapp/controller/action?extras=stuff. To remove the subdirectory prefix we need to find the length of it. That is found by subtracting the length of the script name (retrieved frombasename()) from the length of the script’s name relative to the www root.