I struggle constantly with the best way to structure my applications. The problem is that there is no single structure or even pattern that works across the board. I have finally come up with a way that works pretty well. I say the route out loud. If the sentence makes sense then the structure likely does.
For example, say I have a user controller with an update action. update user makes perfect sense. On the other hand say I want to fetch a user as JSON for the UI and created a json action. json user does not make much sense, since the action name should be a verb (when was the last time you jsoned a user). A better approach would be to add an open action (open user) and then enable the user to specify the output format.
Thursday, February 17, 2011
Tuesday, February 15, 2011
How to add a Subversion external on Windows with Tortoise SVN
I often work on consulting projects where I am integrating my code library with existing Zend Framework projects. I don't like to make the client's team install a pile of libraries; it is much easier to create a single standalone application.
I used to export a version of my working lib into the library folder (standard ZF directory structure) and work off that, but manually updating the library can be a pain. Fortunately subversion has just the tool; externals.
Setting up externals in windows is very simple with Tortoise SVN. First go to the folder that you want to add the external resource to. Then open Tortoise properties ( right click > Tortoise SVN > Properties ).
Once the properties dialog is open click New. Then select svn:externals from the property name selector and enter the path to your repo ( Dirname https://path/to/repo... ) as the property value.
Then OK your way out and update the folder that you added the external to. It will load the files and you are good to go.
Keeping Zend View Helpers organized
Over the last couple years I have accumulated a pretty large closet full of view helpers. Some of them are specific to certain projects, but many of them (my favorites) are much more flexible and still very useful
I have almost as many ways to organize them as I do helpers. They are all more or less the same idea (similar helpers grouped in folders) but have some fundamental differences depending on the way I structured my apps.
I ultimately resorted to a system where each module (in Digitalus Framework) has its own view helper dir, as the standard describes, and then I added a naming convention where the function name had the module name prepended. This works well and is similar to the way ZF does other things, but leaves something to be desired.
At the core we are all professional typists. The more words you type the more work you get done. So the fastest typist (all other things equal) is the best programmer. This oversimplification is probably true, unless you actively strive to minimize the amount that you have to type.
The solution I am using right now is a (very simple) view helper which helps me load the helper packages that I need, so I can eliminate the extraneous prefixes and what not. I also think it makes the code much easier to follow.
<?php
/**
* this helper makes it easier to include the core view helper packages of the dig library
*
*/
class Dig_View_Helper_Using extends Zend_View_Helper_Abstract {
public function using($package) {
$package = ucfirst($package);
$baseHelperPath = realpath(dirname(__FILE__));
$packagePath = $baseHelperPath . '/' . $package;
$this->view->addHelperPath($packagePath, 'Dig_View_Helper_' . $package);
}
}
<?php
/**
* this helper makes it easier to include the core view helper packages of the dig library
*
*/
class Dig_View_Helper_Using extends Zend_View_Helper_Abstract {
public function using($package) {
$package = ucfirst($package);
$baseHelperPath = realpath(dirname(__FILE__));
$packagePath = $baseHelperPath . '/' . $package;
$this->view->addHelperPath($packagePath, 'Dig_View_Helper_' . $package);
}
}
Then I load the necessary view helpers in the head of the view script:
<?php
$this->using('content');
?>
And then get to use nice short function names for the helpers:
<?= $this->textile($instructions);?>
Monday, February 14, 2011
Why use Zend Forms
Zend_Form has always been a pain in my side. I adopted it for a couple years but never really liked it; I like things that make my life easier and I always thought it was easier to just write a form using html and view helpers. There is more to Zend_Form, of course, but all I want a form to do is post data to my server.
Handling Data
My main issue with Zend_Form is that I never agreed with the philosophy of a form handling validation and filtering for a model; I think it is the model's responsibility. I prefer this approach because you never know where data might be coming from. Say you want to add a record to an activity_feed table when you add a new post to the a blog. If you do this in the model after you save the post you need to do one of the following:
- hope you remember all of the required fields for the feed
- write some sort of validation code
- create an instance of the form, populate it, and then add the feed item if its valid
ActiveRecord provides support for automating validation which works very much like my hand rolled versions and just makes more sense to me; it won't allow you to save an invalid row regardless of where it came from.
Rendering Forms
Zend_Form does make basic rendering easy; the form class implements a __toString() method, so you can just print it. Once you need anything different you have two options:
- remember how to whole decorator system works (you are going to need to write decorators, register them, and then specify them)
- write the form in HTML and echo each of the controls individually
I usually end up having to do the latter, wondering why I don't just use view helpers.
I'm sure the form component makes some automated systems easier to manage, but for simple custom software I'm back to handling it myself.
How to handle file uploads with Zend Framework
My first experience with file uploads and Zend Framework came through Zend_Form. I am starting to move away from using Zend Form and Db in favor of active record and forms which I build up in views. The why and how of this move are another story, but suffice to say that I needed a secure way to handle file uploads. Enter Zend_File_Transfer.
Zend_File_Transfer is the component that handles transfer, validation, and filtering of file uploads for Zend_Form. You can use it directly for the same purpose as I'll demo here.
Note: I am assuming that you posted a form with a file control named 'photo'
The first thing you do is create an instance and set the destination directory:
$upload = new Zend_File_Transfer();
$upload->setDestination('/path/to/photos/folder');
You can then add a stack of validators to the upload:
$upload->addValidator('Size', false, 20000);
$upload->addValidator('Extension', false,
'jpg,jpeg,png,gif,bmp');
Zend_File_Transfer is the component that handles transfer, validation, and filtering of file uploads for Zend_Form. You can use it directly for the same purpose as I'll demo here.
Note: I am assuming that you posted a form with a file control named 'photo'
The first thing you do is create an instance and set the destination directory:
$upload = new Zend_File_Transfer();
$upload->setDestination('/path/to/photos/folder');
You can then add a stack of validators to the upload:
$upload->addValidator('Size', false, 20000);
$upload->addValidator('Extension', false,
'jpg,jpeg,png,gif,bmp');
Now you validate and recieve the file:
if($upload->isValid('photo')) {
$upload->receive('photo');
}
Monday, January 31, 2011
SEO trickery is a waste of everyone's time
SEO trickery has been an important business for some years, but I for one think it is on its way out. Its a victim of its own success; the scammers have gotten good enough that they fill the top pages of search results rendering the search useless.
Web design is a good example. Your odds of getting to the top position of a Google search like 'small business web design' are something like 98 million to 1, about as good as winning the lottery and retiring on Wednesday. I'm not saying search engines are obsolete in any way, but you can't afford to focus your effort solely on them anymore.
At Digitalus we are taking a new look at the flow of prospective clients. We get a lot of anonymous traffic on our sites, but get almost all of our work from people we know (or have at least talked to). Our websites are equally important, but get moved to the second step of the process. The thought process here is that a conversation is a more effective way to create a relationship. Once we know each other we direct them to our sites, which shine at efficiently conveying the specific information that the person is looking for.
Subscribe to:
Posts (Atom)

