BcSite::findCurrentSub PHP Method

findCurrentSub() public static method

現在のサイトとユーザーエージェントに関連するサブサイトを取得する
public static findCurrentSub ( boolean $sameMainUrl = false, BcAgent $agent = null, $lang = null ) : BcSite | null
$sameMainUrl boolean
$agent BcAgent
return BcSite | null
    public static function findCurrentSub($sameMainUrl = false, BcAgent $agent = null, $lang = null)
    {
        $currentSite = self::findCurrent();
        $sites = self::findAll();
        $subSite = null;
        if (!$lang) {
            $lang = BcLang::findCurrent();
        }
        if (!$agent) {
            $agent = BcAgent::findCurrent();
        }
        // 言語の一致するサブサイト候補に絞り込む
        $subSites = [];
        if ($lang) {
            foreach ($sites as $site) {
                if (!$sameMainUrl || $sameMainUrl && $site->sameMainUrl) {
                    if ($site->lang == $lang->name && $currentSite->id == $site->mainSiteId) {
                        $subSites[] = $site;
                        break;
                    }
                }
            }
        }
        if (!$subSites) {
            $subSites = $sites;
        }
        if ($agent) {
            foreach ($subSites as $subSite) {
                if (!$sameMainUrl || $sameMainUrl && $subSite->sameMainUrl) {
                    if ($subSite->device == $agent->name && $currentSite->id == $subSite->mainSiteId) {
                        return $subSite;
                    }
                }
            }
        }
        return null;
    }

Usage Example

 /**
  * 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;
     }
 }
All Usage Examples Of BcSite::findCurrentSub