Last active
September 3, 2018 18:13
-
-
Save moo-im-a-cow/60b6407872803488c2a6fcb5ffac7dd9 to your computer and use it in GitHub Desktop.
klein.php subdomain matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$this->respond( | |
'/?', function ($req, $res, $service, $app) { | |
return "home page for SUBDOMAIN"; | |
} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//this is what i changed my code to after seeing this issue: https://github.com/klein/klein.php/issues/245 | |
//see index_original.php for my original code | |
$klein = new \Klein\Klein(); | |
$routesites = array(); | |
$routesites["/^example\.com/"] = "someroute.php"; | |
$routesites["/^sub\.example\.com/"] = "anotherroute.php"; | |
$routesites["error"] = "route404.php"; | |
$host = $_SERVER["HTTP_HOST"]; | |
$route = $routesites["error"]; | |
foreach($routesites as $match=>$route_){ | |
if($match == "error") continue; //skip the error route | |
if(preg_match($match, $host)){ | |
$route = $route_; | |
break; | |
} | |
} | |
$klein->with("/?", $route); | |
$klein->dispatch(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//This is my original version of the code | |
$klein = new \Klein\Klein(); | |
$routesites = array(); | |
$routesites["example.com"] = "someroute.php"; | |
$routesites["hello.example.com"] = "anotherroute.php"; | |
$routesites["error"] = "route404.php"; | |
$host = $_SERVER["HTTP_HOST"]; | |
$route; | |
if(($route = $routesites[$host]) == false){ | |
$route = "error"; | |
} | |
$klein->with("/?", $route); | |
$klein->dispatch(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$this->respond(function ($req, $res, $service, $app) { | |
echo "i think you are lost"; | |
exit; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$this->respond( | |
'/?', function ($req, $res, $service, $app) { | |
return "home page for main site"; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment