It’s handy to use in Zend FW driven project. For example, you want to make an in-place tracker (e.g. Google Analytics) — you create a helper class My_View_Helper_Tracker inherited from Zend_View_Helper, Zend finds its automatically and then you are free to use your helper method:
echo $this->tracker( $trackerID );
The question is what you gonna do if you want a base class for a family of trackers?
It’s not so obvoius due to naming conventions.
Let’s say you want 2 kinds of trackers: Google Analytics and Euroads.
1. You create such files structure:
library
- My
- View
- Helper
- Tracker
Abstract.php
- Google
Page.php
- Euroads
Owner.php
Guest.php
Now let’s create a SQL function for handy converts. Create a udf.sql file and add this in it:
DELIMITER //
DROP FUNCTION IF EXISTS EXCHANGE;
CREATE FUNCTION EXCHANGE( amount DOUBLE, cFrom CHAR(3), cTo CHAR(3) ) RETURNS DOUBLE READS SQL DATA DETERMINISTIC
COMMENT 'converts money amount from one currency to another'
BEGIN
DECLARE rateFrom DOUBLE DEFAULT 0;
DECLARE rateTo DOUBLE DEFAULT 0;
SELECT `rate` INTO rateFrom FROM `currency` WHERE `code` = cFrom;
SELECT `rate` INTO rateTo FROM `currency` WHERE `code` = cTo;
IF ISNULL( rateFrom ) OR ISNULL( rateTo ) THEN
RETURN NULL;
END IF;
RETURN amount * rateTo / rateFrom;
END; //
DELIMITER ;
and run this command in your shell:
mysql --user=USER --password=PASS DATABASE < udf.sql
This is how you can use this function — how to convert 10 US dollars to Canadian dollars:
SELECT EXCHANGE( 10, 'USD', 'CAD')
which results in $10 = 10.93 Canadian dollars.
P.S. Consider adding the currency export action call to your cron scripts.
P.P.S. A function to unzip the data file can be got at
I want to share a couple of features I use to handle AJAX requests in projects based on Zend Framework.
1. AJAX request handling
What: some parts of your application can be not loaded if currect request is AJAX.
Why: you don’t need views, templates, some routes — so you can add an AJAX check in your Initializer or bootstrap file and avoid loading not necessary things.
How: Zend Request object has a to find out whether it’s AJAX request or not. It’s based on ‘X-Requested-With‘ header, which is sent by jQuery, Prototype, Scriptaculous, YUI and MochiKit frameworks.
2. AJAX Controller
Most AJAX controller’s methods I saw had an exit() inside to not to output Zend’s template — it is a work-around. The proper way to do so is to tell to Zend not to load anything. One step forward is to create an abstract Controller class and inherit all you AJAX classes from it:
/library/Koodix/Controller/Ajax/Action.php:
<?php
require_once 'Zend/Controller/Action.php';
abstract class Koodix_Controller_Ajax_Action
extends Zend_Controller_Action
{
public function init() {
//disable the standard layout output
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
}
public function postDispatch() {
//envelope and output json field
if( !empty( $this->json ) ) {
echo json_encode( $this->json );
}
}
}
<?php
class AjaxController extends Koodix_Controller_Ajax_Action
{
// bla-bla-bla
Take a look at postDispatch method — idea behind it is to convert to JSON and output anything that is set to json field of your controller. If you want to send JSON data in special header (and not in body, like it’s done in my example), you can do it in this method.
A problem comes on stage when a Flash file uploader is added to your project – usually it cannot “login” to your site, i.e. users are not able to use the Flash file uploader behind beta login.
That’s how I solved it.
It’s not the web server who must solve this (Apache), it’s the application server (PHP). So remove the lines above from .htaccess and use for this purpose — it’s Zend’s HTTP Authentication Adapter.
What concerns the Flash uploader: it sends ‘Shockwave Flash’ as value of ‘User-Agent’ request header. So in your Initializer or Bootstrap file (where you load Zend_Auth_Adapter_Http) check this header value, and if it’s not Flash’s, go for HTTP authentication.
P.S. Hackers can assume this and fake the header to access your site. To cope with that, use an additional secret request variable (Flash uploaders allow this) and check it at server side.
After authentication in my project it takes about 10-20 minutes for the auth session to expire, which is not handy — you go to get a snack and see a login screen coming back.
This is how to make the TTL of your auth session longer:
Recent Comments