Created
October 6, 2017 12:18
-
-
Save wlkns/3d4939606b59eb616c4a3944b976db63 to your computer and use it in GitHub Desktop.
Laravel region selector (with cookies)
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 | |
namespace App\Providers; | |
use App\Entities\Region; | |
use App\Entities\Site; | |
use Illuminate\Support\Facades\Cookie; | |
use Illuminate\Support\Facades\Request; | |
use Illuminate\Support\Facades\View; | |
use Illuminate\Support\ServiceProvider; | |
class RegionServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
if ( ! app()->runningInConsole() ) | |
{ | |
View::share('region', $this->app->make('region')); | |
} | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->singleton('region', function ($app) { | |
$regionCode = null; | |
// Determine if the region was passed as a URL parameter. | |
if ( Request::hasCookie('region') ) | |
{ | |
$regionCode = Request::cookie('region'); | |
} | |
// If $regionCode is still null, its never been set, lets see if we can look it up. | |
if ( is_null($regionCode) ) | |
{ | |
// @todo IP Lookup - Determine region | |
} | |
return Region::where('code', $regionCode)->first() ?: null; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment