BcSite::findByUrl PHP Method

findByUrl() public static method

public static findByUrl ( $url )
    public static function findByUrl($url)
    {
        $sites = self::findAll();
        $url = preg_replace('/(^\\/|\\/$)/', '', $url);
        $urlAry = explode('/', $url);
        for ($i = count($urlAry); $i > 0; $i--) {
            foreach ($sites as $site) {
                if (implode('/', $urlAry) == $site->alias) {
                    return $site;
                }
            }
            unset($urlAry[$i - 1]);
        }
        return null;
    }

Usage Example

 /**
  * Reverse route
  *
  * @param array $url Array of parameters to convert to a string.
  * @return mixed either false or a string URL.
  */
 public function match($url)
 {
     // フロント以外のURLの場合にマッチしない
     if (!empty($url['admin'])) {
         return false;
     }
     // プラグイン確定
     if (empty($url['plugin'])) {
         $plugin = 'Core';
     } else {
         $plugin = Inflector::camelize($url['plugin']);
     }
     // アクション確定
     if (!empty($url['action'])) {
         $action = $url['action'];
     } else {
         $action = 'index';
     }
     $params = $url;
     unset($params['plugin']);
     unset($params['controller']);
     unset($params['action']);
     unset($params['entityId']);
     // コンテンツタイプ確定、できなければスルー
     $type = $this->_getContentTypeByParams($url);
     if ($type) {
         unset($params['action']);
     } else {
         $type = $this->_getContentTypeByParams($url, false);
     }
     if (!$type) {
         return false;
     }
     // エンティティID確定
     $entityId = $contentId = null;
     if (isset($url['entityId'])) {
         $entityId = $url['entityId'];
     } else {
         $request = Router::getRequest(true);
         if (!empty($request->params['entityId'])) {
             $entityId = $request->params['entityId'];
         }
         if (!empty($request->params['Content']['alias_id'])) {
             $contentId = $request->params['Content']['id'];
         }
     }
     // コンテンツ確定、できなければスルー
     if ($type == 'Page') {
         $pass = [];
         foreach ($params as $key => $param) {
             if (!is_string($key)) {
                 $pass[] = $param;
                 unset($params[$key]);
             }
         }
         $strUrl = '/' . implode('/', $pass);
     } else {
         $Content = ClassRegistry::init('Content');
         if ($contentId) {
             $conditions = ['Content.id' => $contentId];
         } else {
             $conditions = ['Content.plugin' => $plugin, 'Content.type' => $type, 'Content.entity_id' => $entityId];
         }
         $strUrl = $Content->field('url', $conditions);
     }
     if (!$strUrl) {
         return false;
     }
     // URL生成
     $site = BcSite::findByUrl($strUrl);
     if ($site && $site->useSubDomain) {
         $strUrl = preg_replace('/^\\/' . preg_quote($site->alias, '/') . '\\//', '/', $strUrl);
     }
     $pass = [];
     $named = [];
     $setting = Configure::read('BcContents.items.' . $plugin . '.' . $type);
     if (!$params) {
         if (empty($setting['omitViewAction']) && $setting['routes']['view']['action'] != $action) {
             $strUrl .= '/' . $action;
         }
     } else {
         if (empty($setting['omitViewAction'])) {
             $strUrl .= '/' . $action;
         }
         foreach ($params as $key => $param) {
             if (!is_numeric($key)) {
                 if ($key == 'page' && !$param) {
                     $param = 1;
                 }
                 if (!is_array($param)) {
                     $named[] = $key . ':' . $param;
                 }
             } else {
                 $pass[] = $param;
             }
         }
     }
     if ($pass) {
         $strUrl .= '/' . implode('/', $pass);
     }
     if ($named) {
         $strUrl .= '/' . implode('/', $named);
     }
     return $strUrl;
 }
All Usage Examples Of BcSite::findByUrl