Vlastní router – 2× Nette\Application\Request
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- MC
- Člen | 7
Ahoj / Dobrý den,
potřeboval bych pomoc – ve vlastní implementaci routeru, který odkazuje na DB tabulku se mi na základě slugu vrací všechny parametry (modul, presenter, locale, apod.)
Vše funguje do doby než rozšířím aplikaci o uživatelský modul – v tu chvíli například při přihlášení uživatele mi metoda match vrátí 2× parametry. Ty první projdou správně, ale skončím na druhých, kde se vrátí jen akce (například tedy login).
BDump Nette\Application\Request … OK – například služby:
$parameters
Nette\Application\Request #7fa7
method private => "FORWARD" (7)
flags private => array ()
name private => "front:Services" (14)
params private => array (6)
presenter => "Services" (8)
module => "front" (5)
action => "default" (7)
id => 11
slug => "services" (8)
locale => "en" (2)
post private => array ()
files private => array ()
BDump Nette\Application\Request … ŠPATNÉ v části administrace – login:
$parameters
Nette\Application\Request #d525
method private => "FORWARD" (7)
flags private => array ()
name private => "admin:Administration" (20)
params private => array (6)
presenter => "Administration" (14)
module => "admin" (5)
action => "login" (5)
id => 34
slug => "administrace-prihlaseni" (23)
locale => "cs" (2)
post private => array ()
files private => array ()
$parameters
Nette\Application\Request #bf0e
method private => "FORWARD" (7)
flags private => array ()
name private => "admin:Administration" (20)
params private => array (1)
action => "login" (5)
post private => array ()
files private => array ()
Vlastní router:
class CustomRouter extends Route {
const DEFAULT_MODULE = 'front';
const DEFAULT_PRESENTER = 'Homepage';
const DEFAULT_ACTION = 'default';
const DEFAULT_ID = NULL;
/** @var Model\RouterManager */
private $routerManager;
/** @var Kdyby\Translation\Translator */
private $translator;
public function __construct(\App\Model\RouterManager $routerManager, Translator $translator) {
$this->routerManager = $routerManager;
$this->translator = $translator;
parent::__construct('[<locale=cs cs|en>/]<slug>[/<module>][/<presenter>][/<action>][/<id>]', array(
'slug' => array(
self::VALUE => null,
self::FILTER_IN => null,
self::FILTER_OUT => null,
),
'module' => self::DEFAULT_MODULE,
'presenter' => self::DEFAULT_PRESENTER,
'action' => self::DEFAULT_ACTION,
'id' => self::DEFAULT_ID,
));
}
public function match(HttpRequest $httpRequest) {
$appRequest = parent::match($httpRequest);
//dump($appRequest);exit;
if ($appRequest === null) {
throw new BadRequestException;
}
$parameters = $appRequest->getParameters();
//dump($parameters['slug']);exit;
$page = $this->routerManager->getUrls($parameters['slug']);
//dump($page);exit;
if (!$page) {
throw new BadRequestException;
} else {
$this->translator->locale = $page->lang;
$request = new \Nette\Application\Request(
$page->module . ':' . $page->presenter, $httpRequest->getMethod(), $page = array(
'presenter' => $page->presenter,
'module' => $page->module,
'action' => $page->action,
'id' => $page->id,
'slug' => $page->slug,
'locale' => $this->translator->locale,
), $httpRequest->getPost(), $httpRequest->getFiles(), array(\Nette\Application\Request::SECURED => $httpRequest->isSecured())
);
}
//dump($request);
return $request;
}
public function constructUrl(Request $appRequest, Url $refUrl) {
Debugger::barDump($appRequest, '$parameters');
//dump($appRequest, '$parameters');
$pars = $appRequest->getParameters();
//dump($this->translator->locale);exit;
//bdump($pars);
$route = clone($this->routerManager);
$page = $route->findUrl()
->where('id', $pars['id'])
->where('module', $pars['module'])
->where('presenter', $pars['presenter'])
->where('action', $pars['action'])
->where('lang', $pars['locale'])
->fetch();
//dump($page);exit;
if (!$page) {
throw new BadRequestException;
}
//dump($page);exit;
$par = array(
'locale' => $page->lang,
'presenter' => $page->presenter,
'module' => $page->module,
'action' => $page->action,
'id' => $page->id,
'slug' => $page->slug,
);
//bdump($par);exit;
//Debugger::barDump($par, 'PAR');
$appRequest->setParameters($par);
$url = $refUrl->getScheme() . '://' . $refUrl->getHost() . $refUrl->getPath() . $page->slug;
//Debugger::barDump($url, 'URL');
return $url;
}
}
Předem děkuji za pomoc,