Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/293309-using-any-router-from-a-subdirectory/
Share on other sites

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.

->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 by nik_jain

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).

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();
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.