Created
November 20, 2013 19:38
-
-
Save RSully/7569611 to your computer and use it in GitHub Desktop.
Some subdomain stuff for laraval from http://www.reddit.com/r/laravel/comments/1ql5ov/subdomain_routing/cde78sp
This file contains 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 | |
App::before(function($request) | |
{ | |
// determine the subdomain of the current request | |
$server = explode('.', Request::server('HTTP_HOST')); | |
$sansSubdomain = implode('.', array_slice($server, 1)); | |
// there are 3 parts to the domain (i.e. user.domain.com) | |
if (count($server) == 3) | |
{ | |
// first part of array is the subdomain, duh | |
$subdomain = $server[0]; | |
// check if it's "www" (or similar / mistyped variant) | |
if (in_array($subdomain, array('w', 'ww', 'www', 'wwww'))) | |
{ | |
// route somewhere or set a config variable | |
// or do nothing because the next condition is where the magic happens | |
} | |
else if (! is_null($subdomain)) // possibly a meaningful subdomain but we're not sure yet | |
{ | |
// match against your predefined list here | |
$predefinedArray = ['a', 'b', 'c', 'd']; // or whatever you need to look up | |
$predefinedResults = \ModelName::where('someValue', $subdomain)->first(); // obviously fake | |
// if there's a valid matching predefined result | |
if (!count($predefinedResults)) | |
{ | |
Session::flash('error', $subdomain.' is not a valid subdomain.'); | |
return Redirect::to('http://'.$sansSubdomain); | |
} | |
// if this code gets executed, the subdomain is kosher | |
// do other data-juggling here if needed | |
} | |
else // subdomain is invalid or just catch-all depending on your preceding logic | |
{ | |
// present error or handle appropriately for your needs | |
} | |
} | |
else // no apparent subdomain detected | |
{ | |
// do nothing here, will just route normally | |
// be careful of creating redirect loops | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment