osb/app/Http/Middleware/SetSite.php
2021-06-29 16:40:54 +10:00

45 lines
1017 B
PHP

<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Closure;
use App\Models\Site;
/**
* Class SetSite
* This class is responsible for setting our site settings based on the URL of a request
*
* @package App\Http\Middleware
*/
class SetSite
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// @todo Figure out how to know if this is an API call - and deny it if it's not in the database.
$so = new Site;
if ($so->getTable() AND Schema::hasTable($so->getTable()))
$so = Site::where('url',$request->root())->first();
// If we dont exist, we'll return a fake model.
if ((! $so) || (! $so->exists))
$so = (new Site)->sample();
// Set who we are in SETUP.
Config::set('SITE_SETUP',$so);
View::share('so',$so);
return $next($request);
}
}