Archive for August, 2009
Zend_Acl + Zend_Auth + Zend_Controller_Plugin = HAPPY!
Aug 3rd
Ok, so after playing around with Zend_Acl for what seems like forever I’ve finally cracked what feels like the perfect marriage between Zend_Acl, Zend_Auth and an application.
You may ask yourself, why the fuck is it so hard? The simple answer is this…
Zend_Aclwas designed in such a way that it does not require any particular backend technology such as a database or cache server for storage of the ACL data. Its complete PHP implementation enables customized administration tools to be built uponZend_Aclwith relative ease and flexibility.
Ease and flexibility my arse! This is the reason quite a few Zend Developers are just pulling their hair out day in day out. The documentation at zendframework.com by far is the most confusing I’ve ever seen and quite a bit of it is outdated. It’s actually easier reading through the API documentation and trolling through the code than it is reading this stuff. I truelly feel sorry for people using ZF in anything less than ZendStudio or an IDE without the code completion. Authentication and Authorisation are pretty key aspect of any application, if they made it simple for people to set up they wouldn’t need a 2 paragraph section saying how to store the data by serializing it. Any way, rant over!
The Solution
There are many ways to store the ACL data for persistence, but I think that’s the problem. As soon as you hear the words store and persistance the bells and whistles in your head are screaming at you telling you to store it in a database just like in CakePHP. For months I’d been trying to work out a way to do this until last night.
Why not put this in the application configuration (application.ini)? This is application configuration after all! What’s the point in taking a hit on your database every time you want to check authorisation and then having to write caching code to counteract that? It’s counterproductive and not cost effective (thanks Kennedy stole your phrase).
We can then read the configuration from ini file which contains all of our Resources and Roles.
To begin I set my self some conditions:
- Whatever the solution is, it has to be decoupled from the rest of the application apart from the models used for the user obviously.
- It has to be piss easy to implement
- It has to be so easy to implement my mum could do it!
PHP Variable Variables
Aug 3rd
Whilst I don’t advocate assigning variable variables, there are times when you need to create variables in PHP dynamically.
What is a Variable Variable?
A variable variable is a variable that can be set dynamically. For instance the normal way to create a variable in PHP would be:
$foo = 'bar';
But what happens when you’re setting a variable dynamically, i.e. it doesn’t exist already and it’s dependant on user input or an unpredictable event? The solution… a Variable Variable
$foo = 'bar'; $$foo = 'hello'; echo $bar; // this will output 'hello'
This may seem useless to you but after just over 5 years of programming I’d finally found a sane some what safe use for it. I had an object that has a property that contained an array of data, I wanted to be able to access that data from within an included file in my function. To do this I had to assign a load of variables that were dynamically set within the object… you know what, let me just show you!
class Template {
protected $variables = array();
public function set($key, $value){
if($key === 'this'){
throw new Exception('Key cannot be set to "this"');
}
$this->variables[$key] = $value;
}
/**
* Outputs the fully rendered view
*
* @param bool $output
*/
public function output($output = true){
$this->_loadTemplate();
$this->output = ob_get_clean();
if($output === true){
echo $this->output;
}
}
private function _loadTemplate(){
foreach($this->variables as $key => $value){
$$key = $value;
}
require_once 'layout/' . $this->layout . '.phtml';
}
}
So now in my layout, instead of having to do this to get my template variables..
<?php echo $this->variables['foo'] ?>
I can use this
<?php echo $foo; ?>
All done, also as a side note, people who use
<?=$fool; ?>
to output content, stop it! You’re shooting your self in the foot, when you migrate to a server that doesn’t support short tags and you can’t change it, you’ll be fucked and annoyed!
Use
<?php echo $foo; ?>
Instead, the extra key strokes saves the weeks of headache!
JS – Adapting to the situation (smart coding) with Portlet’s
Aug 3rd
Preface
For the past few months I’ve been workin with a content management portal to deliver my XHTML/JS/CSS I can’t stress how big a pain in the arse this is, as with any CMS there are limitations in the way that it works and problems when you try to use it for more than what it’s really made for. Limitations of a CMS can include:
- not being able to add code/css to the header
- the CMS just spitting out any old JS/CSS in the HTML
- HTML not being formatted correctly, or just spitting out random XHTML tags
The list could go on, but I’m too tired to really carry on. It puts a big downer on you most of all when you’re so anal that all of your HTML is well formated and perfectly indented, only to find when the site goes live, all of your code is on one line or it looks like it’s been run over by a bus.
My main concern with coding for CMS’s that use portlet’s is the simple fact that each portlet is in essence it’s own self contained application/controller. It becomes a pain when you have to constantly check to see whethter a portlet exists in the DOM on runtime.
How can you work around this? Think like a CMS. You might be thinking, what the f**k is he talking about? The simple solution is to adapt the way you code to suit the CMS. Instead of having one big JS file for a page or site section, seperate your code into small chunks, this will also help you unit test your code which I’ll come to a bit later.

