How to pass parameters from Router FILTER_IN function into Presenter?
- petroff
- Member | 5
Hi there, would somebody help me with this please?
I am using FILTER_IN function in Router to look in database for a presenter
related to current URL.
For example:
www.domain.com/some-category/ is treated by presenter –
category. There are other information also stored in the database (like Title,
Description of page – all managed by users) what I’d like to pass into
Presenter – category.
Then at the end I need to access the original some-category to perform
FILTER_OUT function.
Can you please advice where and how to store such data?
Many thanks for your help!
Router with FILTER_IN function Process
<?php
/**
* Router factory.
*/
class RouterFactory
{
/** @var Model\Process */
private $process;
public function __construct(Model\Process $process)
{
$this->process= $process;
}
/**
* @return \Nette\Application\IRouter
*/
public function createRouter()
{
$router = new RouteList();
$router[] = new Route('<presenter>[/<action>][/<id>]', array(
'presenter' => array(
Route::FILTER_IN => callback($this->process, 'getpresin'),
Route::FILTER_OUT => callback($this->process, 'getpresout')
),
'action' => 'default',
'id' => NULL,
));
$router[] = new Route('<presenter>/<action>[/<id>]', 'Homepage:default');
return $router;
}
?>
FILTER_IN function ‚getpresin‘ is reading database ‘process’ to obtain presenter – ‘ProcessPres’ and this is returned back to router. Works fine.
In the method – ‘getpresin’ I need to save somewhere other information from ‘process’ file like ‘ProcessPres’, ‘ProcessTitle', 'ProcessDesc’… to be able to access it in presenter.
And finally at the end I need to have again original ‘ProcessPres’ value available in FILTER_OUT function – ‘getpresout’ – to be able to route back to /some-category/
<?php
class Process extends Nette\Object
{
/** @var Nette\Database\Context */
private $database;
public function __construct(Nette\Database\Context $database)
{
$this->database = $database;
}
// Get Presenter from DB----------------------------------------------------------
public function getpresin ($dir) {
$datavar = $this->database->query('SELECT ProcessPres, ProcessDirTxt, ProcessTitle, ProcessDesc FROM process WHERE ProcessDir1= ? LIMIT 1', $dir);
$row = $datavar->fetch();
$pres = $row["ProcessPres"];
// Where and how to store $row to be able to acces it in presenter and also in FILTER_OUT function getpresout?;
return $pres;
}
// Get Presenter to call from DB----------------------------------------------------------
public function getpresout ($dir) {
//return the original “some-category” into $pres
return $pres;
}
?>
- petr.pavel
- Member | 535
Calls to FILTER_IN and FILTER_OUT must work independently. You can't assume FILTER_IN will be called before FILTER_OUT. Think of a link in a page. No FILTER_IN, just FILTER_OUT. (I hope I'm not confusing the two.)
What info do you need in your FILTER_OUT function?
As for accessing a db result fetched in FILTER_IN also in presenter:
- Define method getById() in say, ProcessModel class.
- Implement caching into getById() (e.g. using a static variable / array with id as the index).
- Use $processModel->getById() in both your FILTER_IN function and the presenter.