ErrorPresenter a 404 page na Nette 2.3.4
- Joacim
- Člen | 229
Zdravím,
stáhnul jsem si Nette ve verzi 2.3.4. s tím, že adresářová struktura zůstala nezměněna.
config.neon
#
# WARNING: it is CRITICAL that this file & directory are NOT accessible directly via a web browser!
# https://nette.org/en/security-warning
#
parameters:
php:
date.timezone: Europe/Prague
application:
errorPresenter: Error
mapping:
*: App\*Module\Presenters\*Presenter
extensions:
translation: Kdyby\Translation\DI\TranslationExtension
session:
expiration: 2 days
services:
router: App\RouterFactory::createRouter
ErrorPresenter
<?php
namespace App\Presenters;
use Nette;
use App\Model;
use Tracy\ILogger;
class ErrorPresenter extends BasePresenter
{
/** @var ILogger */
private $logger;
public function __construct(ILogger $logger)
{
$this->logger = $logger;
}
/**
* @param Exception
* @return void
*/
public function renderDefault($exception)
{
if ($exception instanceof Nette\Application\BadRequestException) {
$code = $exception->getCode();
// load template 403.latte or 404.latte or ... 4xx.latte
$this->setView(in_array($code, array(403, 404, 405, 410, 500)) ? $code : '4xx');
// log to access.log
$this->logger->log("HTTP code $code: {$exception->getMessage()} in {$exception->getFile()}:{$exception->getLine()}", 'access');
} else {
$this->setView('500'); // load template 500.latte
$this->logger->log($exception, ILogger::EXCEPTION); // and log exception
}
if ($this->isAjax()) { // AJAX request? Note this error in payload.
$this->payload->error = TRUE;
$this->terminate();
}
}
}
Bootstrap
<?php
require __DIR__ . '/../vendor/autoload.php';
$configurator = new Nette\Configurator;
// Disable Debugger ("Laděnka")
$configurator->setDebugMode(true);
//$configurator->setDebugMode('23.75.345.200'); // enable for your remote IP
$configurator->enableDebugger(__DIR__ . '/../log');
$configurator->setTempDirectory(__DIR__ . '/../temp');
$configurator->createRobotLoader()
->addDirectory(__DIR__)
->register();
$configurator->addConfig(__DIR__ . '/config/config.neon');
$configurator->addConfig(__DIR__ . '/config/config.local.neon');
$container = $configurator->createContainer();
return $container;
S tím že zadám špatnou (neexistující stránku) a dostanu: Hlášku na
neexistující presenter
Zkoušel jsem i
common:
nette:
application:
errorPresenter: "Error"
- není definován nette a common
Také jsem zkoušel v bootstrapu nastavit:
$application->catchExceptions = TRUE;
i
$application = Environment::getApplication();
...
$application->errorPresenter = 'Error';
$application->catchExceptions = TRUE;
- v obou případech jsem dostal hlášku, že promenná(session) byla již nastavena
Určitě je to jen banalita, ale zatím jsem nepřišel na způsob jak 404 zprovoznit.
- Lukeluha
- Člen | 130
Používáš nějaké sekce common atd. To se od 0.9 nepoužívá. Pokud se podíváš do sandboxu, uvidíš, že error presenter se nastavuje pomocí
application:
errorPresenter: Error
EDIT: beru zpět, nevšimnul jsem si, že v configu to již máš, akorát se tam snažíš narvat nějaké sekce navíc :) Pokud ti to nefunguje ani v čistém sandboxu, zkus vytvořit nový presenter a jen na něj přejít (bez error presenteru). Pokud ti ani to nebude fungovat, řekl bych, že nemáš povolený mod_rewrite
Editoval Lukeluha (31. 7. 2015 8:32)
- Joacim
- Člen | 229
Zkoušel jsem vícero verzí, bylo toho opravdu hafo, procházel jsem i všechny diskuze zde na nette a zkoušel a experimentoval, ale zatím bezúspěšně, mod_rewrite povolen mám, mám web na localhostu a používám alias a pomocí htaccess si nastavuji basedir, ale pro jistotu na to kouknu, každopádně routování atd mi vše funguje bez problémů
Editoval Joacim (31. 7. 2015 8:37)
- Joacim
- Člen | 229
Error presenter mi již „funguje“ (viz. níže)
application:
catchExceptions: true
mapping:
*: App\*Module\Presenters\*Presenter
pokud zadám adresu která neexistuje např:
localhost/projekt/galerie/xyz12
Vyrenderuje se mi přes celou stránku
Page Not Found
The page you requested could not be found. It is possible that the address is incorrect, or that the page no longer exists. Please use a search engine to find what you are looking for.
error 404
Bohužel nevím odkud se tato zpráva bere, jelikož přes ErrorPresenter.php nic neprojde (var_dump + exit) a pokud změním stránku 404 která je v /presenters/templates/Error/404.latte – nikde se změna neprojeví (stále stejná hláška)
namespace App\Presenters;
use Nette;
use App\Model;
use Tracy\ILogger;
class ErrorPresenter extends BasePresenter {
/** @var ILogger */
private $logger;
public function __construct(ILogger $logger) {
$this->logger = $logger;
}
/**
* @param Exception
* @return void
*/
public function renderDefault($exception) {
if ($exception instanceof Nette\Application\BadRequestException) {
$code = $exception->getCode();
// load template 403.latte or 404.latte or ... 4xx.latte
$this->setView(in_array($code, array(403, 404, 405, 410, 500)) ? $code : '4xx');
// log to access.log
$this->logger->log("HTTP code $code: {$exception->getMessage()} in {$exception->getFile()}:{$exception->getLine()}", 'access');
} else {
$this->setView('500'); // load template 500.latte
$this->logger->log($exception, ILogger::EXCEPTION); // and log exception
}
if ($this->isAjax()) { // AJAX request? Note this error in payload.
$this->payload->error = TRUE;
$this->terminate();
}
}
}