findCurrentSub() public static method

現在のサイトとユーザーエージェントに関連するサブサイトを取得する
public static findCurrentSub ( boolean $sameMainUrl = false, BcAgent $agent = null, $lang = null ) : BcSite | null
$sameMainUrl boolean
$agent BcAgent
return BcSite | null
 /**
  * Before Dispatch
  *
  * @param CakeEvent $event containing the request and response object
  * @return void
  */
 public function beforeDispatch(CakeEvent $event)
 {
     $request = $event->data['request'];
     $response = $event->data['response'];
     if ($request->is('admin')) {
         return;
     }
     $subSite = BcSite::findCurrentSub();
     if (!is_null($subSite) && $subSite->shouldRedirects($request)) {
         $response->header('Location', $request->base . $subSite->makeUrl($request));
         $response->statusCode(302);
         return $response;
     }
 }
 /**
  * Parses a string URL into an array. If a plugin key is found, it will be copied to the
  * controller parameter
  *
  * @param string $url The URL to parse
  * @return mixed false on failure, or an array of request parameters
  */
 public function parse($url)
 {
     if (is_array($url)) {
         return false;
     }
     if (BcUtil::isAdminSystem($url)) {
         return false;
     }
     $request = Router::getRequest(true);
     //管理システムにログインしているかつプレビューの場合は公開状態のステータスは無視する
     $publish = true;
     if (!empty($request->query['preview']) || !empty($request->query['force'])) {
         $publish = false;
     }
     $sameUrl = false;
     $site = BcSite::findCurrentSub(true);
     if ($site) {
         // 同一URL対応
         $sameUrl = true;
         $checkUrl = $site->makeUrl($request);
         @header('Vary: User-Agent');
     } else {
         $site = BcSite::findCurrent(true);
         if (!is_null($site->name)) {
             if ($site->useSubDomain) {
                 $checkUrl = '/' . $site->alias . ($url ? $url : '/');
             } else {
                 $checkUrl = $url ? $url : '/';
             }
         } else {
             if (!empty($request->query['force']) && BcUtil::isAdminUser()) {
                 // =================================================================================================
                 // 2016/11/10 ryuring
                 // 別ドメインの際に、固定ページのプレビューで、正しくサイト情報を取得できない。
                 // そのため、文字列でリクエストアクションを送信し、URLでホストを判定する。
                 // =================================================================================================
                 $tmpSite = BcSite::findByUrl($url);
                 if (!is_null($tmpSite)) {
                     $site = $tmpSite;
                 }
             }
             $checkUrl = $url ? $url : '/';
         }
     }
     $content = $this->getContent($checkUrl, $publish, false, $sameUrl, $site->useSubDomain);
     if (!$content) {
         $content = $this->getContent($checkUrl, $publish, true, $sameUrl, $site->useSubDomain);
     }
     if (!$content) {
         return false;
     }
     // データが存在してもプレビューで管理システムにログインしていない場合はログイン画面に遷移
     if ((!empty($request->query['preview']) || !empty($request->query['force'])) && !BcUtil::loginUser()) {
         $_SESSION['Auth']['redirect'] = $_SERVER['REQUEST_URI'];
         header('Location: ' . topLevelUrl(false) . baseUrl() . Configure::read('BcAuthPrefix.admin.alias') . '/users/login');
         exit;
     }
     $Content = ClassRegistry::init('Content');
     if ($content['Content']['alias_id'] && !$Content->isPublishById($content['Content']['alias_id'])) {
         return false;
     }
     $request->params['Content'] = $content['Content'];
     $request->params['Site'] = $content['Site'];
     $url = $site->getPureUrl($url);
     $params = $this->getParams($url, $content['Content']['url'], $content['Content']['plugin'], $content['Content']['type'], $content['Content']['entity_id'], $site->alias);
     if ($params) {
         return $params;
     }
     return false;
 }
 /**
  * After Render
  *
  * @param string $viewFile
  */
 public function afterRender($viewFile)
 {
     parent::afterRender($viewFile);
     if (BcUtil::isAdminSystem()) {
         return;
     }
     if (empty($this->request->params['Site'])) {
         return;
     }
     if (isset($this->request->params['Site']['name']) && is_null($this->request->params['Site']['name'])) {
         return;
     }
     if (isset($this->request->params['Site']['device']) && $this->request->params['Site']['device'] != '') {
         return;
     }
     // 別URLの場合、alternateを出力(スマートフォンのみ対応)
     $pureUrl = $this->BcContents->getPureUrl($this->request->url, $this->request->params['Site']['id']);
     $agent = BcAgent::find('smartphone');
     $subSite = BcSite::findCurrentSub(false, $agent);
     if (!$subSite) {
         return;
     }
     $url = $subSite->makeUrl(new CakeRequest($pureUrl));
     $this->_View->set('meta', $this->BcHtml->meta('canonical', $this->BcHtml->url($url, true), ['rel' => 'canonical', 'media' => 'only screen and (max-width: 640px)', 'type' => null, 'title' => null, 'inline' => false]));
 }