Фаза на красном, жёлтый видимо ноль, и отсоединенный синий есть, вероятно земля, но я не проверял.
namespace app\base;
use dictionary\models\Average;
use dictionary\models\Features;
use dictionary\models\Kitchen;
use dictionary\models\Position;
use dictionary\models\Type;
use dictionary\models\TypeFacility;
use restaurants\models\Restaurants;
use structure\models\Section;
use tops\models\Collection;
/**
* Class UrlManager
*
* @package app\base
*/
class UrlManager extends \yii\web\UrlManager
{
/**
* @var
*/
public $domain;
/**
* @var
*/
public $original;
/**
* @var array
*/
private $_params = [];
/**
* @param \yii\web\Request $request
* @return mixed
*/
public function parseRequest($request)
{
if (false !== $r = parent::parseRequest($request)) {
return $r;
}
$result = false;
$name = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
/* @var Restaurants $rest */
if (($rest = \Yii::$app->db->cache(function ($db) use ($name) {return Restaurants::findOne(['domain' => $name, 'publish' => 1]);}, 3600))) {
$this->_params['rest'] = $rest;
$this->_params['city'] = $rest->city;
if ($request->url === '/') {
$this->_params['section'] = \Yii::$app->db->cache(function ($db) use ($rest) {
return Section::findOne(['rest_id' => $rest->id, 'depth' => 0, 'publish' => 1]);
}, 3600);
$result = ['restaurants/frontend/default', []];
} else {
$result = $this->parseStruct($request, $rest);
}
} elseif ($dict = Average::findOne(['domain' => $name, 'publish' => 1])) {
if ($request->url === '/') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'average';
$result = ['dictionary/frontend/default', []];
} elseif ($request->url === '/robots.txt') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'average';
$result = ['dictionary/frontend/default/robots', []];
}
} elseif ($dict = Kitchen::findOne(['domain' => $name, 'publish' => 1])) {
if ($request->url === '/') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'kitchen';
$result = ['dictionary/frontend/default', []];
} elseif ($request->url === '/robots.txt') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'kitchen';
$result = ['dictionary/frontend/default/robots', []];
}
} elseif ($dict = Type::findOne(['domain' => $name, 'publish' => 1])) {
if ($request->url === '/') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'type';
$result = ['dictionary/frontend/default', []];
} elseif ($request->url === '/robots.txt') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'type';
$result = ['dictionary/frontend/default/robots', []];
}
} elseif ($dict = Position::findOne(['domain' => $name, 'publish' => 1])) {
if ($request->url === '/') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'position';
$result = ['dictionary/frontend/default', []];
} elseif ($request->url === '/robots.txt') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'position';
$result = ['dictionary/frontend/default/robots', []];
}
} elseif ($dict = TypeFacility::findOne(['domain' => $name, 'publish' => 1])) {
if ($request->url === '/') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'type_facility';
$result = ['dictionary/frontend/default', []];
} elseif ($request->url === '/robots.txt') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'type_facility';
$result = ['dictionary/frontend/default/robots', []];
}
} elseif ($dict = Features::findOne(['domain' => $name, 'publish' => 1])) {
if ($request->url === '/') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'features';
$result = ['dictionary/frontend/default', []];
} elseif ($request->url === '/robots.txt') {
$this->_params['dictionary'] = $dict;
$this->_params['model_type'] = 'features';
$result = ['dictionary/frontend/default/robots', []];
}
} elseif ($top = Collection::findOne(['domain' => $name, 'publish' => 1])) {
if ($request->url === '/' || $request->url === '/all') {
$this->_params['collection'] = $top;
$this->_params['model_type'] = 'collection';
$result = ['tops/frontend/top/index', []];
} elseif ($request->url === '/robots.txt') {
$this->_params['collection'] = $top;
$this->_params['model_type'] = 'collection';
$result = ['tops/frontend/top/robots', []];
}
} elseif (($route = $this->parseStruct($request))) {
$section = $this->_params['section'];
if (strpos($name, $section->city->name) === false) {
$qr = !empty($request->queryString) ? "?" . $request->queryString : "";
$url = sprintf("http://%s.%s%s", $section->city->name, \Yii::$app->params['domain'], $section->url, $qr);
// \Yii::$app->response->redirect($url, 301);
header("Location: {$url}");
// TODO убрать если что-то сломается
die();
}
$result = $route;
}
return $result;
}
/**
* @return array
*/
public function getParams()
{
return $this->_params;
}
/**
* @param \yii\web\Request $request
* @param Restaurants $rest
* @return mixed
*/
private function parseStruct($request, $rest = null)
{
$pathInfo = trim($request->pathInfo, '/');
$query = Section::find();
if ($pathInfo === '') {
$query->where(['publish' => 1, 'depth' => 0]);
} elseif ($pathInfo === 'robots.txt') {
$request->pathInfo = 'robots';
} else {
$segments[] = '/' . $pathInfo . '/';
while (($pos = strrpos($pathInfo, '/')) !== false) {
$pathInfo = substr($pathInfo, 0, $pos);
$segments[] = '/' . $pathInfo . '/';
}
$query->where(['publish' => 1, 'url' => $segments])
->orderBy(['lft' => SORT_DESC, 'depth' => SORT_DESC]);
}
if (isset($rest)) {
$query->andWhere(['rest_id' => $rest->id]);
}
$section = \Yii::$app->db->cache(function ($db) use ($query) {
return $query->one();
}, 3600);
if (isset($section)) {
$this->_params['section'] = $section;
$this->_params['city'] = isset($rest->city_id) ? $rest->city_id : 'krasnodar';
$route = trim(str_replace($section->url, '/', '/' . $request->pathInfo . '/'), '/');
if ($route === '') {
$request->pathInfo = $section->route;
$route = $section->route;
} else {
$route = substr($section->route, 0, strrpos($section->route, '/') + 1) . $route;
$request->pathInfo = $route;
}
$this->_params['route'] = $route;
$this->enableStrictParsing = false;
if (false !== $r = parent::parseRequest($request)) {
$this->enableStrictParsing = true;
return $r;
}
}
return $request->pathInfo;
}
}
/**
* @return null|Robots
*/
protected function getRobots()
{
$url = $_SERVER['HTTP_HOST'];
$restName = strtok($url, '.');
$rest = Restaurants::findOne(['name' => $restName]);
$robots = Robots::findOne(['rest_id' => $rest->id]);
return $robots;
}
/**
* @return string
*/
public function actionRobots()
{
$response = \Yii::$app->response;
$response->format = Response::FORMAT_RAW;
$response->getHeaders()->set('Content-Type', 'text/plain; charset=utf-8');
if ($this->getRobots()) {
return $this->renderPartial('robots', ['content' => $this->getRobots()->content]);
}
$content = <<<TEXT
User-agent: *
Allow: *
Host: {$_SERVER['HTTP_HOST']}
TEXT;
return $this->renderPartial('robots', ['content' => $content]);
}
Жать кнопку все равно придется, потому что в клоаку надо отправить запрос. А вот уже клоака сама поймет, если юзер уже авторизован - отдаст токены, если нет - попросит ввести пару логин/пароль.
Если кратко, то: