nik_jain Posted December 23, 2014 Share Posted December 23, 2014 I was trying to use one of the many PHP routers like klein, when I kept hitting an issue. The issue was my root folder was a subdirectory of the www folder . So instead of requesting http://localhost/index.php (as one would if the index.php was located in /var/www) I was requesting http://localhost/issues/web/index.php (index.php in /var/www/issues/web) Naturally the router looked for a match for the whole path(issues/web/index.php) instead of just the relative path(/index.php) Which caused it not to match anything ! Questions: 1. Is this a viable solution for this problem (Got this from https://github.com/chriso/klein.php/wiki/Sub-Directory-Installation? Any possible gotchas I should be on the look out for ? $base = dirname($_SERVER['PHP_SELF']); // Update request when we have a subdirectory if(ltrim($base, '/')){ $_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], strlen($base)); } //initialize router code. 2. This must be an extremely common situation! How do people deal with this ? OR is it simply not common for routers to be installed to subfolders and called from there ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/293309-using-any-router-from-a-subdirectory/ Share on other sites More sharing options...
requinix Posted December 23, 2014 Share Posted December 23, 2014 1. It's bad form to modify values in the superglobals so try to avoid that. It looks like klein has a ->with() that would work for this. $klein->with("/issues/web", function() use($klein) { $klein->respond("GET", "/index.php", function($request, $response) { ... }; }For the "regular" case you may be able to do $klein->with("", function() use($klein) { ... });However can't you just base your site out of /var/www/issues/web instead? 2. Depends on the router. Quote Link to comment https://forums.phpfreaks.com/topic/293309-using-any-router-from-a-subdirectory/#findComment-1500527 Share on other sites More sharing options...
nik_jain Posted December 24, 2014 Author Share Posted December 24, 2014 (edited) ->with() works nicely! Thanks $base = dirname($_SERVER['PHP_SELF']) ; if ($base === '/' || $base === '\\'){ $base = '' ; } $klein = new \Klein\Klein(); $klein->with($base, function() use($klein) { $klein->respond('GET', '/hello', function () { return 'Hello World!'; }); }); $klein->dispatch(); However can't you just base your site out of /var/www/issues/web instead? by hardcoding into .htaccess ? I kind of wanted a 'redistributable' solution.. Edited December 24, 2014 by nik_jain Quote Link to comment https://forums.phpfreaks.com/topic/293309-using-any-router-from-a-subdirectory/#findComment-1500555 Share on other sites More sharing options...
requinix Posted December 24, 2014 Share Posted December 24, 2014 Then that's getting into "configuration" territory. You need to expose to your end-user a place where they can specify the base URL, then you use that value where needed (eg, the with() and when constructing URLs). Quote Link to comment https://forums.phpfreaks.com/topic/293309-using-any-router-from-a-subdirectory/#findComment-1500577 Share on other sites More sharing options...
nik_jain Posted December 25, 2014 Author Share Posted December 25, 2014 I see.. So I guess another way of saying what I want to ask is 'how to detect the BASE_URL automatically ?' If memory serves right then some frameworks do try to detect the base URL automatically while also giving an option to set it manually. ok googling around I found this: http://blog.lavoie.sl/2013/02/php-document-root-path-and-url-detection.html But I think the following way should also work, with any router. I don't see any gotchas in this, but there might be. $base = dirname($_SERVER['PHP_SELF']) ; if ($base === '/' || $base === '\\'){ $base = '' ; } $klein = new \Klein\Klein(); $klein->respond('GET', $base.'/hello', function () { return 'Hello World!'; }); $klein->dispatch(); Quote Link to comment https://forums.phpfreaks.com/topic/293309-using-any-router-from-a-subdirectory/#findComment-1500593 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.