Pimcore\Model\Site::getByDomain PHP Method

getByDomain() public static method

public static getByDomain ( $domain ) : mixed | Site | string
$domain
return mixed | Site | string
    public static function getByDomain($domain)
    {
        // cached because this is called in the route (Pimcore_Controller_Router_Route_Frontend)
        $cacheKey = "site_domain_" . md5($domain);
        if (!($site = \Pimcore\Cache::load($cacheKey))) {
            $site = new self();
            try {
                $site->getDao()->getByDomain($domain);
            } catch (\Exception $e) {
                Logger::debug($e);
                $site = "failed";
            }
            \Pimcore\Cache::save($site, $cacheKey, ["system", "site"]);
        }
        if ($site == "failed" || !$site) {
            $msg = "there is no site for the requested domain [" . $domain . "], content was [" . $site . "]";
            Logger::debug($msg);
            throw new \Exception($msg);
        }
        return $site;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @param \Zend_Controller_Request_Abstract $request
  */
 public function routeStartup(\Zend_Controller_Request_Abstract $request)
 {
     // this is a filter which checks for common used files (by browser, crawlers, ...) and prevent the default
     // error page, because this is more resource-intensive than exiting right here
     $found = false;
     foreach (self::$files as $pattern) {
         if (preg_match($pattern, $request->getPathInfo())) {
             $found = true;
             break;
         }
     }
     if ($found) {
         if ($request->getPathInfo() == "/robots.txt") {
             // check for site
             try {
                 $domain = Tool::getHostname();
                 $site = Site::getByDomain($domain);
             } catch (\Exception $e) {
             }
             $siteSuffix = "";
             if ($site instanceof Site) {
                 $siteSuffix = "-" . $site->getId();
             }
             // send correct headers
             header("Content-Type: text/plain; charset=utf8");
             while (@ob_end_flush()) {
             }
             // check for configured robots.txt in pimcore
             $robotsPath = PIMCORE_CONFIGURATION_DIRECTORY . "/robots" . $siteSuffix . ".txt";
             if (is_file($robotsPath)) {
                 readfile($robotsPath);
             } else {
                 echo "User-agent: *\nDisallow:";
                 // default behavior
             }
             exit;
         }
         // if no other rule matches, exit anyway with a 404, to prevent the error page to be shown
         header('HTTP/1.1 404 Not Found');
         echo "HTTP/1.1 404 Not Found\nFiltered by common files filter";
         exit;
     }
 }
All Usage Examples Of Pimcore\Model\Site::getByDomain