Final Class luya\cms\frontend\Bootstrap

Inheritanceluya\cms\frontend\Bootstrap
Implementsyii\base\BootstrapInterface
Available since version1.0.0
Source Code https://github.com/luyadev/luya-module-cms/blob/master/src/frontend/Bootstrap.php

CMS Bootstrap.

The CMS bootstrap class injects the cms specific url rules

  • {{luya\cms\frontend\components\RouteBehaviorUrlRule}}
  • {{luya\cms\frontend\components\CatchAllUrlRule}}

And changes the behavior if an exception appears in order to redirect users to a custom cms page.

Public Methods

Hide inherited methods

Method Description Defined By
bootstrap() Bootstrap luya\cms\frontend\Bootstrap

Method Details

Hide inherited methods

bootstrap() public method

Bootstrap

public void bootstrap ( $app )
$app luya\web\Application

                public function bootstrap($app)
{
    if ($app->hasModule('cms')) {
        // load cms url rules
        $app->on(Application::EVENT_BEFORE_REQUEST, function ($event) {
            if (!$event->sender->request->isConsoleRequest && !$event->sender->request->isAdmin) {
                $rules = [
                    ['class' => 'luya\cms\frontend\components\RouteBehaviorUrlRule'],
                    ['class' => 'luya\cms\frontend\components\CatchAllUrlRule'],
                ];
                if ($event->sender->getModule('cms')->enableWebsiteHostRedirect) {
                    array_unshift($rules, ['class' => 'luya\cms\frontend\components\WebsiteBehaviorUrlRule']);
                }
                $event->sender->urlManager->addRules($rules);
            }
        });
        // handle not found exceptions
        $app->errorHandler->on(ErrorHandler::EVENT_BEFORE_EXCEPTION_RENDER, function (ErrorHandlerExceptionRenderEvent $event) use ($app) {
            if ($app instanceof Application && $event->exception instanceof NotFoundHttpException && !$app->request->isAdmin) {
                $errorPageNavId = Config::get(Config::HTTP_EXCEPTION_NAV_ID, 0);
                // if not defined abort.
                if (!$errorPageNavId) {
                    return;
                }
                /** @var $item Item */
                $item = $app->menu->find()->with(['hidden'])->where(['nav_id' => $errorPageNavId])->one();
                // unable to find the item, maybe its offline or does not exists anymore.
                if (!$item) {
                    return;
                }
                // render CMS 404 page
                $app->menu->setCurrent($item);
                $result = $app->runAction('cms/default/index');
                if ($result instanceof Response) {
                    $response = $result;
                } else {
                    $response = new Response();
                    $response->data = $result;
                }
                $response->setStatusCodeByException($event->exception);
                $app->end(1, $response);
                exit;
            }
        });
    }
}