Skip to content

Instantly share code, notes, and snippets.

@moo-im-a-cow
Last active September 3, 2018 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moo-im-a-cow/60b6407872803488c2a6fcb5ffac7dd9 to your computer and use it in GitHub Desktop.
Save moo-im-a-cow/60b6407872803488c2a6fcb5ffac7dd9 to your computer and use it in GitHub Desktop.
klein.php subdomain matching
<?php
$this->respond(
'/?', function ($req, $res, $service, $app) {
return "home page for SUBDOMAIN";
}
);
<?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();
<?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();
<?php
$this->respond(function ($req, $res, $service, $app) {
echo "i think you are lost";
exit;
});
<?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