Contao\PageModel::findPublishedByIdOrAlias PHP Method

findPublishedByIdOrAlias() public static method

Find published pages by their ID or aliases
public static findPublishedByIdOrAlias ( mixed $varId, array $arrOptions = [] ) : Collection | PageModel[] | PageModel | null
$varId mixed The numeric ID or the alias name
$arrOptions array An optional options array
return Contao\Model\Collection | PageModel[] | PageModel | null A collection of models or null if there are no pages
    public static function findPublishedByIdOrAlias($varId, array $arrOptions = array())
    {
        $t = static::$strTable;
        $arrColumns = array("({$t}.id=? OR {$t}.alias=?)");
        $arrValues = array(is_numeric($varId) ? $varId : 0, $varId);
        if (isset($arrOptions['ignoreFePreview']) || !BE_USER_LOGGED_IN) {
            $time = \Date::floorToMinute();
            $arrColumns[] = "({$t}.start='' OR {$t}.start<='{$time}') AND ({$t}.stop='' OR {$t}.stop>'" . ($time + 60) . "') AND {$t}.published='1'";
        }
        return static::findBy($arrColumns, $arrValues, $arrOptions);
    }

Usage Example

Example #1
0
 /**
  * Run the controller
  *
  * @return Response
  *
  * @throws PageNotFoundException
  */
 public function run()
 {
     $pageId = $this->getPageIdFromUrl();
     $objRootPage = null;
     // Load a website root page object if there is no page ID
     if ($pageId === null) {
         $objRootPage = $this->getRootPageFromUrl();
         /** @var PageRoot $objHandler */
         $objHandler = new $GLOBALS['TL_PTY']['root']();
         $pageId = $objHandler->generate($objRootPage->id, true, true);
     } elseif ($pageId === false) {
         $this->User->authenticate();
         throw new PageNotFoundException('Page not found: ' . \Environment::get('uri'));
     }
     $pageModel = \PageModel::findPublishedByIdOrAlias($pageId);
     // Throw a 404 error if the page could not be found
     if ($pageModel === null) {
         throw new PageNotFoundException('Page not found: ' . \Environment::get('uri'));
     }
     return $this->renderPage($pageModel);
 }
All Usage Examples Of Contao\PageModel::findPublishedByIdOrAlias