mercoledì 11 maggio 2011

Zend howto redirect user to the desider page

In this article i'll explain how zend session hops work.

Let's assume you have a page the user wants to access, but it requires a login first or some other action. Most web pages just redirect you to the login and then you have to go find the page you wanted to see.

Step 1: open a page that has some prerequisite



Step 2: you get an ugly error

The page that showed you this error saves the URL you really wanted to visit.
For example the code in a static function would be :

$lastPg = new Zend_Session_Namespace('ditta_redirect');
$lastPg->url = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
$lastPg->setExpirationHops(2);
      
self::$flashMessenger->addMessage(array('alert', self::$translate->translate('per questa operazione devi prima selezionare una ditta') . "\n"));
self::$redirector->gotoSimple("index", "index");

The user then logs in (or chooses something)
 


After you process the page, you load the saved url and redirect the user to the page he initially wanted to see.
This is the code
$namespace = new Zend_Session_Namespace('ditta_redirect');
$redirect_after_login_url = $namespace->url;
if ($redirect_after_login_url == "/user/login" || $redirect_after_login_url == "/user/logout")
 $redirect_after_login_url = "/index/index";

if (!empty($redirect_after_login_url))
 $this->_helper->redirector->gotoUrl($redirect_after_login_url);
      

Tadaaa you finally got to the inital requested page.






However note a feature. If the user doesn't choose something, but clicks on any other link, 
then the saved url will be deleted since he will go beyond 2 hops(= 2 page redirects). In this way, if he chooses that something in the future, he will not be redirected to the saved url(since there will be no redirected URL), but will remain on the same page.

martedì 10 maggio 2011

phpMyAdmin vs MySql console

Say one day(Is It Friday Yet?) you feel lazy and yet you have to optimize some sql queries that are a bit slow.

Typically that means you have to go find the mysql.exe, find the right parameters to give it, remember the username and password for the root(don't use the root user :P ). Finally you run an explain query and you get this


The HORROR, you can't resize horizontally only verically and the rows are collapsed. Before you try and think about a linux shell, stop and think about the benefits of WAMP(e.g. XamppEasyPhp).

They all install phpMyAdmin for you. So you just type in your query and click the explain button (on the right of the page under the textarea)



In optimizing this ugly query by eliminating the where ... in i also had to add some indexes. Again avoid the console and just use the nice GUI.


i highlighted the index i just added

So to optimize use your favourite book(if you have one) or just read a tutorial.

Finally you should get this beauty :)
 



have fun and KISS

lunedì 9 maggio 2011

Why use try - catch inside the controllers?

So you have a nice MVC website.

All is perfect until one of your models throws an error
If you use use Zend Framework you will get this ugly error :



So let's help the user. Inside our controller we have to put the whole body of each myactionAction()  into a try - catch and add the error's message to the Zend Flash Messenger(while you are at it you might even translate it to a user friendly message). Then just redirect to a page you know it will not throw any errors and in the view you use jQuery UI CSS  and print it to get this nice picture :)


that's all