Zend Doctrine Session SaveHandler. How make it work? - zend-framework

I am trying to understand how to set up a Session Save Handler (with Zend/Doctrine) using a database table but I am a bit confused about how it all should work.
I found this proposal that I think it suits my needs as I am also working with Doctrine.
All is set up: the proper class, the database table and Doctrine Model. What I don't get is this part:
$config = array(
'tableName' => 'Session',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime',
'modifiedColumn' => 'modified',
'primaryKeyColumn' => 'id',
);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_Doctrine($config));
Zend_Session::start();
I am confused here. Where should this part go? Can anyone please help? Or maybe point me to some useful tutorial to do this?

This should go in your main bootstrap class (application/Bootstrap.php). So I'd add something like this:
protected function _initDoctrineSession()
{
$config = array(
'tableName' => 'Session',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime',
'modifiedColumn' => 'modified',
'primaryKeyColumn' => 'id',
);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_Doctrine($config));
Zend_Session::start();
}

Related

Zend Framework 2 invalidate translator cache

I have this code:
'translator' => array(
...
'cache' => array(
'adapter' => array(
'name' => 'Filesystem',
'options' => array(
'cache_dir' => __DIR__ . '/../../../data/cache',
'ttl' => '3600'
)
),
'plugins' => array(
array(
'name' => 'serializer',
'options' => array()
),
'exception_handler' => array(
'throw_exceptions' => true
)
)
)
The question is, how do I invalidate it not by TTL?
For example, I KNOW when the translation was changed so I want to invalidate in on demand but I have not found a way to do it.
The translator component does not utilize the TaggableInterface so you have to know the cacheId which the translator generates to clear the item from you storage adapter. You can use the following code to simply generate the same id and remove the item. Call this from your service or some event listener.
$translator = $sm->get('McvTranslator');
$textDomain = 'default';
$locale = 'en';
$cacheId = 'Zend_I18n_Translator_Messages_' . md5($textDomain . $locale);
$translator->getCache()->removeItem($cacheId);
I think you could set Ttl = 0 (always), and when the cache (file) is not valid anymore -- delete it.
Another way to do it:
Find a point in your code where you call addTranslation.
For example:
$translate = Zend_Registry::get('Zend_Translate');
$translate->addTranslation(array(
'content' => "$dir/$locale.mo",
'locale' => $locale
));
Change the addTranslation function to add reload => true , like this:
$translate->addTranslation(array(
'content' => "$dir/$locale.mo",
'locale' => $locale,
'reload' => true
));
Refresh your page.
Voila.
Remeber to remove reload after that, otherwise you will have no cache.

Can't manage to make a static page with Zend Framework (about us)

I'm trying to make a website using the Zend Framework because I heard it was really good and I'd like to experiment with frameworks, but I have a simple problem driving me crazy.
I'd like to make a simple mywebsite.com/about page with static content because there is no need to do anything else than display html.
I made an About controller, the phtml associated with it, but when I try to go to /about, I get a 404 error followed by:
The requested controller could not be mapped to an existing controller class.
Controller:
not-found(resolves to invalid controller class or alias: not-found)
So I checked my routing file:
'router' => array(
'routes' => array(
'about' => array(
'type' => 'segment',
'options' => array(
'route' => '/about',
),
'defaults' => array(
'controller' => 'About\Controller\About',
'action' => 'index',
),
),
),
),
so if I'm not wrong this is supposed to call the About controller that I made, but can't manage to find the problem.
If someone could help me understand what I missed or what I didn't get with Zend, I will be very glad.
EDIT: The version of Zend is 2.x if that changes anything
#
EDIT 2: I found the solution thanks to the help of James Kent
My routing file seemed to be wrong, here is the new one :
'router' => array(
'routes' => array(
'about' => array(
'type' => 'Literal',
'options' => array(
'route' => '/about',
'defaults' => array(
'__NAMESPACE__' => 'About\Controller',
'controller' => 'About',
'action' => 'index',
),
),
),
),
),
I also had to change the indexAction of my About controller:
class AboutController extends AbstractActionController
{
public function nolayoutAction() {
$viewModel = new ViewModel();
//$viewModel->setTerminal(true); Uncomment to disable the layout
return $viewModel;
}
public function indexAction()
{
$viewModel = $this->nolayoutAction();
return $viewModel;
}
}
I hope it will help some people.
From what I can recall, something like this should work:
$route = new Zend_Controller_Router_Route_Static(
'about',
array('controller' => 'about')
);
$router->addRoute('about', $route);
In the meantime, I'm going to look for the documentation where I originally learned about this.
Edit: As it turns out, what you may need are Custom Routes, and their documentation is over here.

Recovering a parameter over the bootstrap Zend Framework

I am developing an application in Zend framework. In my bootstrap I have a method named _initRoutes:
public function _initRoutes(){
$router = $this->_front->getRouter();
$default = new Zend_Controller_Router_Route(':language/:controller/:action/*',
array(
'language' => Model_Db_Option::read('site_default_language'),
'module' => 'public',
'controller' => 'index',
'action' => 'index'
),
array(
'language' => '^[a-z]{2}$*'
)
);
}
Hoy can I recover the language value in the _initRoutes??? How can I get this value in the next function _initLocale
PS. I know that I can recover it as parameter from request but I want to recover it in the bootstrap.
i used this
$params=#$default->match($this->_front->getRequest()->getRequestUri());
$lang=$params['language'];
try
$language = Zend_Controller_Front::getInstance()->getRequest()->getParam('language');
something like this, can't test from this comp

Zend Framework Change parameter in route on the same spot?

I'm not sure how to fix this, or wat is the best way to approach this. Also couldn't find enough information to get me on the right way (could be that my searching sucks..)
Anyway, this is my problem:
I defined a route in my bootstrap file:
protected function _initRoutes()
{
$router = $this->frontController->getRouter();
$router->removeDefaultRoutes();
$router->addRoute(
'delete',
new Zend_Controller_Router_Route('/:controller/:action/:id/',
array('controller' => ':controller',
'action' => ':action',
'id' => ':id',
)
)
);
}
This works perfectly for my update and delete actions.
Now I've added the pagination to the indexpage. The pagination expects the page parameter. Because I haven't set this in my route, it cannot pass it, so my pagination doesn't work (as in switching between results).
I understand this. But what I want is that on the index page the id parameter isn't necessary and replace this with the page parameter.
Trying another route replacing id with page didn't work.
Is there a good way to solve this in the bootstrap or is it the best way to check for the action, and depending on the action, index or update/delete, define the route. The best place would than be a plugin?
Any advice or tips are greatly appreciated!
While working on another aspect of the application I came back to the same problem. I solved it, by specifying the routes much more.
First I deleted the $router->removeDefaultRoutes(); rule.
And then instead of (which didn't work):
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/:action/:page', array('controller' => ':controller', 'action' => ':action', 'page' => ':page'))
);
I now use this:
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/index/:page', array('controller' => ':controller', 'action' => 'index', 'page' => ':page'))
);

Building a modular Website with Zend Framework: Am I on the right way?

I´m a little bit confused by reading all the posts and tutorials about starting with Zend, because there a so many different ways to solve a problem.
I just need some feedback about my code to know if I am on the right track.
To simply get a (hard coded) Navigation for my site (depending on who is logged in) I build a Controller Plugin with a postDispatch method:
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
$menu = new Menu();
//Render menu in menu.phtml
$view = new Zend_View();
//NEW view -> add View Helper
$prefix = 'My_View_Helper';
$dir = dirname(__FILE__).'/../../View/Helper/';
$view->addHelperPath($dir,$prefix);
$view->setScriptPath('../application/default/views/scripts/menu');
$view->menu = $menu->getMenu();
$this->getResponse()->insert('menu', $view->render('menu.phtml'));
}
Is it right that I need to set the helper path again?
I did this in a Plugin Controller named ViewSetup. There I do some setup for the view like doctype, headlinks, and helper paths (This step is from the book: Zend Framework in Action).
The Menu class which is initiated looks like this:
class Menu
{
protected $_menu = array();
/**
* Menu for notloggedin and logged in
*/
public function getMenu()
{
$auth = Zend_Auth::getInstance();
$view = new Zend_View();
//check if user is logged in
if(!$auth->hasIdentity()) {
$this->_menu = array(
'page1' => array(
'label' => 'page1',
'title' => 'page1',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page1'))
),
'page2' => array(
'label' => 'page2',
'title' => 'page2',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page2'))
),
'page3' => array(
'label' => 'page3',
'title' => 'page3',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page3'))
),
'page4' => array(
'label' => 'page4',
'title' => 'page4',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page4'))
),
'page5' => array(
'label' => 'page5',
'title' => 'page5',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page5'))
)
);
} else {
//user is vom type 'client'
//..
}
return $this->_menu;
}
}
Here´s my view script:
<ul id="mainmenu">
<?php echo $this->partialLoop('menuItem.phtml',$this->menu) ?>
</ul>
This is working so far. My question is: is it usual to do it this way; is there anything to improve?
I´m new to Zend and I've seen deprecated tutorials on the web which often are not obvious. Even the book is already deprecated where the autoloader is mentioned.
You shouldn't be creating a new view. Since you have already created the View object in your Boostrap (and used it to render the rest of the site) you should fetch the already created view object.
If you are using Zend_Application_Resource to setup your view in the bootstrap you can fetch it like this:
$view = Zend_Controller_Front::getInstance()
->getParam('bootstrap')
->getResource('view');
This way there is no need to set the view helper path again and create another view object.
If you are not using the Zend_Application to boostrap your app you could try something like this:
$view = Zend_Layout::getMvcInstance()->getView();
Unless you are working on a relatively small side, I wouldn't do this in the controller,
since you will have to add this to many controllers.
Why not check in the bootstrap or even checking in your layout would make more sense to me although it wouldn't be proper.