core-bundle/src/Resources/contao/controllers/BackendSwitch.php
public function run()
	{
		$this->disableProfiler();

		if (Environment::get('isAjaxRequest'))
		{
			$this->getDatalistOptions();
		}

		$objJwtManager = null;

		if ($objRequest = System::getContainer()->get('request_stack')->getCurrentRequest())
		{
			$objJwtManager = $objRequest->attributes->get(JwtManager::ATTRIBUTE);
		}

		$blnCanSwitchUser = ($this->User->isAdmin || (!empty($this->User->amg) && \is_array($this->User->amg)));
		$objTokenChecker = System::getContainer()->get('contao.security.token_checker');
		$strUser = $objTokenChecker->getFrontendUsername();
		$blnShowUnpublished = $objTokenChecker->isPreviewMode();
		$blnDebug = System::getContainer()->get('kernel')->isDebug();
		$blnUpdate = false;

		// Switch
		if (Input::post('FORM_SUBMIT') == 'tl_switch')
		{
			$blnUpdate = true;
			$objAuthenticator = System::getContainer()->get('contao.security.frontend_preview_authenticator');
			$blnShowUnpublished = Input::post('unpublished') != 'hide';
			$blnDebug = (bool) Input::post('debug');

			// Switch user accounts
			if ($blnCanSwitchUser && isset($_POST['user']))
			{
				$strUser = Input::post('user');
			}

			if ($strUser)
			{
				$objAuthenticator->authenticateFrontendUser($strUser, $blnShowUnpublished);
			}
			else
			{
				$objAuthenticator->authenticateFrontendGuest($blnShowUnpublished);
			}
		}

		$objTemplate = new BackendTemplate('be_switch');
		$objTemplate->user = (string) $strUser;
		$objTemplate->show = $blnShowUnpublished;
		$objTemplate->update = $blnUpdate;
		$objTemplate->canSwitchUser = $blnCanSwitchUser;
		$objTemplate->canDebug = $this->User->isAdmin && $objJwtManager instanceof JwtManager;
		$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->debug = $blnDebug;
		$objTemplate->debugMode = $GLOBALS['TL_LANG']['MSC']['debugMode'];
		$objTemplate->lblEnabled = $GLOBALS['TL_LANG']['MSC']['debugEnabled'];
		$objTemplate->lblDisabled = $GLOBALS['TL_LANG']['MSC']['debugDisabled'];
		$objTemplate->action = ampersand(Environment::get('request'));

		$objResponse = $objTemplate->getResponse();

		if ($blnUpdate && $objJwtManager instanceof JwtManager)
		{
			$objJwtManager->addResponseCookie($objResponse, array('debug' => $blnDebug));
		}

		return $objResponse;
	}
public function run()
	{
		$this->disableProfiler();

		if (Environment::get('isAjaxRequest'))
		{
			$this->getDatalistOptions();
		}

		$objJwtManager = null;

		if ($objRequest = System::getContainer()->get('request_stack')->getCurrentRequest())
		{
			$objJwtManager = $objRequest->attributes->get(JwtManager::ATTRIBUTE);
		}

		$blnCanSwitchUser = ($this->User->isAdmin || (!empty($this->User->amg) && \is_array($this->User->amg)));
		$objTokenChecker = System::getContainer()->get('contao.security.token_checker');
		$strUser = $objTokenChecker->getFrontendUsername();
		$blnShowUnpublished = $objTokenChecker->isPreviewMode();
		$blnDebug = System::getContainer()->get('kernel')->isDebug();
		$blnUpdate = false;

		// Switch
		if (Input::post('FORM_SUBMIT') == 'tl_switch')
		{
			$blnUpdate = true;
			$objAuthenticator = System::getContainer()->get('contao.security.frontend_preview_authenticator');
			$blnShowUnpublished = Input::post('unpublished') != 'hide';
			$blnDebug = (bool) Input::post('debug');

			// Switch user accounts
			if ($blnCanSwitchUser && isset($_POST['user']))
			{
				$strUser = Input::post('user');
			}

			if ($strUser)
			{
				$objAuthenticator->authenticateFrontendUser($strUser, $blnShowUnpublished);
			}
			else
			{
				$objAuthenticator->authenticateFrontendGuest($blnShowUnpublished);
			}
		}

		$objTemplate = new BackendTemplate('be_switch');
		$objTemplate->user = (string) $strUser;
		$objTemplate->show = $blnShowUnpublished;
		$objTemplate->update = $blnUpdate;
		$objTemplate->canSwitchUser = $blnCanSwitchUser;
		$objTemplate->canDebug = $this->User->isAdmin && $objJwtManager instanceof JwtManager;
		$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->debug = $blnDebug;
		$objTemplate->debugMode = $GLOBALS['TL_LANG']['MSC']['debugMode'];
		$objTemplate->lblEnabled = $GLOBALS['TL_LANG']['MSC']['debugEnabled'];
		$objTemplate->lblDisabled = $GLOBALS['TL_LANG']['MSC']['debugDisabled'];
		$objTemplate->action = ampersand(Environment::get('request'));

		$objResponse = $objTemplate->getResponse();

		if ($blnUpdate && $objJwtManager instanceof JwtManager)
		{
			$objJwtManager->addResponseCookie($objResponse, array('debug' => $blnDebug));
		}

		return $objResponse;
	}
Run the controller and parse the template

@return Response