Fluent::is_frontend PHP Method

is_frontend() public static method

If viewing in the CMS items filtered by locale will always be visible, but in the frontend will be filtered as expected. For the sake of unit tests Fluent assumes a frontend execution environment.
public static is_frontend ( boolean $ignoreController = false ) : boolean
$ignoreController boolean Flag to indicate whether the current controller should be ignored, and detection should be performed by inspecting the URL. Used for testing. Defaults to false.
return boolean Flag indicating if the translation should act on the frontend
    public static function is_frontend($ignoreController = false)
    {
        // Option to force a frontend response if required
        if (null !== self::$force_is_frontend) {
            return (bool) self::$force_is_frontend;
        }
        // No controller - Possibly pre-route phase, so check URL
        if ($ignoreController || !Controller::has_curr()) {
            if (empty($_SERVER['REQUEST_URI'])) {
                return true;
            }
            // $_SERVER['REQUEST_URI'] indeterminately leads with '/', so trim here
            $base = preg_quote(ltrim(Director::baseURL(), '/'), '/');
            return !preg_match('/^(\\/)?' . $base . 'admin(\\/|$)/i', $_SERVER['REQUEST_URI']);
        }
        // Check if controller is aware of its own role
        $controller = Controller::curr();
        if ($controller instanceof ContentController) {
            return true;
        }
        if ($controller->hasMethod('isFrontend')) {
            return $controller->isFrontend();
        }
        // Default to return false for any CMS controller
        return !$controller instanceof AdminRootController && !$controller instanceof LeftAndMain;
    }

Usage Example

 /**
  * Gets the current locale
  *
  * @param boolean $persist Attempt to persist any detected locale within session / cookies
  * @return string i18n locale code
  */
 public static function current_locale($persist = true)
 {
     // Check overridden locale
     if (self::$_override_locale) {
         return self::$_override_locale;
     }
     // Check direct request
     $locale = self::get_request_locale();
     // Persistant variables
     if (empty($locale)) {
         $locale = self::get_persist_locale();
     }
     // Check browser headers
     if (empty($locale)) {
         $locale = self::detect_browser_locale();
     }
     // Fallback to default if empty or invalid (for this domain)
     $caresAboutDomains = Fluent::is_frontend();
     if (empty($locale) || !in_array($locale, self::locales($caresAboutDomains))) {
         $locale = self::default_locale($caresAboutDomains);
     }
     // Persist locale if requested
     if ($persist) {
         self::set_persist_locale($locale);
     }
     return $locale;
 }
All Usage Examples Of Fluent::is_frontend