diff --git a/src/Application/ErrorPresenter.php b/src/Application/ErrorPresenter.php index e348d5ce4..7a1b40a3b 100644 --- a/src/Application/ErrorPresenter.php +++ b/src/Application/ErrorPresenter.php @@ -9,6 +9,7 @@ use Nette; use Nette\Application; +use Nette\Http; use Tracy\ILogger; @@ -43,8 +44,10 @@ public function run(Application\Request $request) $this->logger->log($e, ILogger::EXCEPTION); } } - return new Application\Responses\CallbackResponse(function () use ($code) { - require __DIR__ . '/templates/error.phtml'; + return new Application\Responses\CallbackResponse(function (Http\IRequest $httpRequest, Http\IResponse $httpResponse) use ($code) { + if (preg_match('#^text/html(?:;|$)#', $httpResponse->getHeader('Content-Type'))) { + require __DIR__ . '/templates/error.phtml'; + } }); } } diff --git a/src/Application/Request.php b/src/Application/Request.php index 63712c254..37ed10bc4 100644 --- a/src/Application/Request.php +++ b/src/Application/Request.php @@ -32,6 +32,9 @@ class Request /** flag */ const RESTORED = 'restored'; + /** flag */ + const VARYING = 'varying'; + /** @var string|null */ private $method; diff --git a/src/Application/Routers/Route.php b/src/Application/Routers/Route.php index 0461256c2..a7ae9b663 100644 --- a/src/Application/Routers/Route.php +++ b/src/Application/Routers/Route.php @@ -728,7 +728,7 @@ private static function renameKeys($arr, $xlat) * @param string * @return string */ - private static function action2path($s) + public static function action2path($s) { $s = preg_replace('#(.)(?=[A-Z])#', '$1-', $s); $s = strtolower($s); @@ -742,7 +742,7 @@ private static function action2path($s) * @param string * @return string */ - private static function path2action($s) + public static function path2action($s) { $s = preg_replace('#-(?=[a-z])#', ' ', $s); $s = lcfirst(ucwords($s)); @@ -756,7 +756,7 @@ private static function path2action($s) * @param string * @return string */ - private static function presenter2path($s) + public static function presenter2path($s) { $s = strtr($s, ':', '.'); $s = preg_replace('#([^.])(?=[A-Z])#', '$1-', $s); @@ -771,7 +771,7 @@ private static function presenter2path($s) * @param string * @return string */ - private static function path2presenter($s) + public static function path2presenter($s) { $s = preg_replace('#([.-])(?=[a-z])#', '$1 ', $s); $s = ucwords($s); @@ -786,7 +786,7 @@ private static function path2presenter($s) * @param string * @return string */ - private static function param2path($s) + public static function param2path($s) { return str_replace('%2F', '/', rawurlencode($s)); } diff --git a/src/Application/UI/Multiplier.php b/src/Application/UI/Multiplier.php index 528e62e34..79a07dccd 100644 --- a/src/Application/UI/Multiplier.php +++ b/src/Application/UI/Multiplier.php @@ -7,7 +7,6 @@ namespace Nette\Application\UI; -use Nette; /** diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 0203fcea8..bbb9f23f6 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -43,6 +43,9 @@ abstract class Presenter extends Control implements Application\IPresenter /** @var int */ public $invalidLinkMode; + /** @var callable[] function (Presenter $sender); Occurs when the presenter is starting */ + public $onStartup; + /** @var callable[] function (Presenter $sender, IResponse $response = null); Occurs when the presenter is shutting down */ public $onShutdown; @@ -180,6 +183,7 @@ public function run(Application\Request $request) $this->initGlobalParameters(); $this->checkRequirements($this->getReflection()); + $this->onStartup($this); $this->startup(); if (!$this->startupCheck) { $class = $this->getReflection()->getMethod('startup')->getDeclaringClass()->getName(); @@ -735,18 +739,20 @@ public function getLastCreatedRequestFlag($flag) */ public function canonicalize($destination = null, array $args = []) { - if (!$this->isAjax() && ($this->request->isMethod('get') || $this->request->isMethod('head'))) { + $request = $this->request; + if (!$this->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) { try { $url = $this->createRequest( $this, $destination ?: $this->action, - $args + $this->getGlobalState() + $this->request->getParameters(), + $args + $this->getGlobalState() + $request->getParameters(), 'redirectX' ); } catch (InvalidLinkException $e) { } if (isset($url) && !$this->httpRequest->getUrl()->isEqual($url)) { - $this->sendResponse(new Responses\RedirectResponse($url, Http\IResponse::S301_MOVED_PERMANENTLY)); + $code = $request->hasFlag($request::VARYING) ? Http\IResponse::S302_FOUND : Http\IResponse::S301_MOVED_PERMANENTLY; + $this->sendResponse(new Responses\RedirectResponse($url, $code)); } } } diff --git a/tests/UI/Presenter.formatLayoutTemplateFiles.phpt b/tests/UI/Presenter.formatLayoutTemplateFiles.phpt index a57a72577..a271e31a1 100644 --- a/tests/UI/Presenter.formatLayoutTemplateFiles.phpt +++ b/tests/UI/Presenter.formatLayoutTemplateFiles.phpt @@ -4,7 +4,6 @@ * Test: Presenter::formatLayoutTemplateFiles. */ -use Nette\Application\UI\Presenter; use Tester\Assert; diff --git a/tests/UI/Presenter.formatTemplateFiles.phpt b/tests/UI/Presenter.formatTemplateFiles.phpt index 2c107436e..c2bd996f7 100644 --- a/tests/UI/Presenter.formatTemplateFiles.phpt +++ b/tests/UI/Presenter.formatTemplateFiles.phpt @@ -4,7 +4,6 @@ * Test: Presenter::formatTemplateFiles. */ -use Nette\Application\UI\Presenter; use Tester\Assert;