Contao\BackendSwitch::run PHP Method

run() public method

Run the controller and parse the template
public run ( ) : Response
return Symfony\Component\HttpFoundation\Response
    public function run()
    {
        $this->disableProfiler();
        if (\Environment::get('isAjaxRequest')) {
            $this->getDatalistOptions();
        }
        $strUser = '';
        $strHash = $this->getSessionHash('FE_USER_AUTH');
        // Get the front end user
        if (FE_USER_LOGGED_IN) {
            $objUser = $this->Database->prepare("SELECT username FROM tl_member WHERE id=(SELECT pid FROM tl_session WHERE hash=?)")->limit(1)->execute($strHash);
            if ($objUser->numRows) {
                $strUser = $objUser->username;
            }
        }
        /** @var BackendTemplate|object $objTemplate */
        $objTemplate = new \BackendTemplate('be_switch');
        $objTemplate->user = $strUser;
        $objTemplate->show = \Input::cookie('FE_PREVIEW');
        $objTemplate->update = false;
        // Switch
        if (\Input::post('FORM_SUBMIT') == 'tl_switch') {
            $time = time();
            // Hide unpublished elements
            if (\Input::post('unpublished') == 'hide') {
                $this->setCookie('FE_PREVIEW', 0, $time - 86400, null, null, \Environment::get('ssl'), true);
                $objTemplate->show = 0;
            } else {
                $this->setCookie('FE_PREVIEW', 1, $time + \Config::get('sessionTimeout'), null, null, \Environment::get('ssl'), true);
                $objTemplate->show = 1;
            }
            // Allow admins to switch user accounts
            if ($this->User->isAdmin) {
                // Remove old sessions
                $this->Database->prepare("DELETE FROM tl_session WHERE tstamp<? OR hash=?")->execute($time - \Config::get('sessionTimeout'), $strHash);
                // Log in the front end user
                if (\Input::post('user')) {
                    $objUser = \MemberModel::findByUsername(\Input::post('user'));
                    if ($objUser !== null) {
                        // Insert the new session
                        $this->Database->prepare("INSERT INTO tl_session (pid, tstamp, name, sessionID, ip, hash) VALUES (?, ?, ?, ?, ?, ?)")->execute($objUser->id, $time, 'FE_USER_AUTH', \System::getContainer()->get('session')->getId(), \Environment::get('ip'), $strHash);
                        // Set the cookie
                        $this->setCookie('FE_USER_AUTH', $strHash, $time + \Config::get('sessionTimeout'), null, null, \Environment::get('ssl'), true);
                        $objTemplate->user = \Input::post('user');
                    }
                } else {
                    // Remove cookie
                    $this->setCookie('FE_USER_AUTH', $strHash, $time - 86400, null, null, \Environment::get('ssl'), true);
                    $objTemplate->user = '';
                }
            }
            $objTemplate->update = true;
        }
        // Default variables
        $objTemplate->theme = \Backend::getTheme();
        $objTemplate->base = \Environment::get('base');
        $objTemplate->language = $GLOBALS['TL_LANGUAGE'];
        $objTemplate->apply = $GLOBALS['TL_LANG']['MSC']['apply'];
        $objTemplate->reload = $GLOBALS['TL_LANG']['MSC']['reload'];
        $objTemplate->feUser = $GLOBALS['TL_LANG']['MSC']['feUser'];
        $objTemplate->username = $GLOBALS['TL_LANG']['MSC']['username'];
        $objTemplate->charset = \Config::get('characterSet');
        $objTemplate->lblHide = $GLOBALS['TL_LANG']['MSC']['hiddenHide'];
        $objTemplate->lblShow = $GLOBALS['TL_LANG']['MSC']['hiddenShow'];
        $objTemplate->fePreview = $GLOBALS['TL_LANG']['MSC']['fePreview'];
        $objTemplate->hiddenElements = $GLOBALS['TL_LANG']['MSC']['hiddenElements'];
        $objTemplate->action = ampersand(\Environment::get('request'));
        $objTemplate->isAdmin = $this->User->isAdmin;
        return $objTemplate->getResponse();
    }

Usage Example

 /**
  * Renders the front end preview switcher.
  *
  * @return Response
  *
  * @Route("/switch", name="contao_backend_switch")
  */
 public function switchAction()
 {
     $this->container->get('contao.framework')->initialize();
     $controller = new BackendSwitch();
     return $controller->run();
 }