[Solar-talk] Method-Based Actions

Travis Swicegood development at domain51.com
Fri Jun 23 11:58:34 PDT 2006


Mike Naberezny wrote:
> Paul M Jones wrote:
>
>   
>>> Personally I wouldn't bother with "action" at all as it's unnecessary.
>>>    
>>>
>>>       
>> I think it's necessary, otherwise display(), fetch(), apiVersion(),  
>> locale(), and other public methods are presented as allowed actions.
>>  
>>
>>     
>
> Also, there is a slight PHP limitation here in that you risk clashing 
> with PHP constructs if there is no prefix or suffix.  A method named 
> "listAction" is acceptable but a method named "list" will result in a 
> parse error for unexpected T_LIST.
>   

The larger, and just as important issue here (imo) is having the ability 
to easily limit which methods are executable by the 
dispatcher/front-controller when passing an action in.

index.php?controller=Bookmarks&action=edit

Could easily be hacked to do

index.php?controller=Bookmarks&action=pseudoprivate

Now, I'll agree that having a pseudo-private on the controller to 
share/generate information between methods is a bad idea, but by the 
very same token I don't see the reason to not have a prefix/suffix of 
some sort or another that the front controller is limited to.  Consider 
it a namespace if you will.  That also makes it much easier for future 
devs that come to the code to figure out what can be accessed via a URL 
and what can't.

As for the naming, I've seen both done and both are legible.  Ideally, 
you won't have any methods on a controller other than the actions 
anyhow, so the documentation argument becomes moot.  Given that there's 
reasons for both, why not handle the method mapping in an object that 
can be replaced. 

interface Solar_Controller_ActionMapper {
    /**
     * Returns the method name based on the action that is provided
     *
     * @param string
     * @return string
     */
    public function getActionName($action);
}

Then create a Solar_Controller_PrefixActionMapper and 
Solar_Controller_SuffixActionMapper and add a config value for 
specifying the name of the ActionMapper you want to use.  Best of both 
worlds and you have another flex point to allow for some other type of 
method mapping we're not thinking of (maybe 
Solar_Controller_DyslexicMapper - getActionName('edit') returns 'tide'). :-D

-Travis


More information about the solar-talk mailing list