Skip to content
Wade Womersley

wade.one

wade womersley – york based software engineer

  • Home
  • 2011
  • April
  • 25
  • Automating “Implemented methods” for a web based API

Automating “Implemented methods” for a web based API

Posted on April 25, 2011 By Wade No Comments on Automating “Implemented methods” for a web based API
Programming

In my leedsphp talk last week I mentioned making a developer (and consumers) life easier by automatically implementing the allow methods functionality that your API may expose (e.g. you call PUT on a URL that only allows GET or POST). I did have an example slide there showing how to implement but I thought I’d posted a bare bones controller here that shows how it works.

abstract class ApiController extends Zend_Rest_Controller
{
    /**
     * Default head action.
     *
     * @internal
     * @return void
     */
    public function headAction()
    {
        ob_start();

        $this->getAction();

        ob_get_clean();
    }

    /**
     * Default get action.
     *
     * @internal
     * @return void
     */
    public function getAction()
    {
        $this->_showAllowedMethods();
    }

    /**
     * Default post action.
     *
     * @internal
     * @return void
     */
    public function postAction()
    {
        $this->_showAllowedMethods();
    }

    /**
     * Default put action.
     *
     * @internal
     * @return void
     */
    public function putAction()
    {
        $this->_showAllowedMethods();
    }

    /**
     * Default delete action.
     *
     * @internal
     * @return void
     */
    public function deleteAction()
    {
        $this->_showAllowedMethods();
    }

    /**
     * Allowed methods output.
     *
     * @internal
     * @return void
     */
    private function _showAllowedMethods()
    {
        $methods = $this->implementedMethods();
        $methods = implode(',', $methods);
        $methods = strtoupper($methods);
        $this->_response->setHeader('Allow', $methods);

        $this->_forward('method-not-allowed', 'error', 'default'); # I have a methodNotAllowedAction on my ErrorController
							           # just for clarity - it does nothing other than headers
    }

    /**
     * Direct call to get implemented methods
     *
     * @internal
     * @return array
     */
    public function implementedMethods()
    {
        if(!isset($this->_actionController))
            return array();

        $class              = get_class($this->_actionController);
        $oReflector         = new ReflectionClass($class);
        $methods            = $oReflector->getMethods(ReflectionMethod::IS_PUBLIC);
        $implementedMethods = array();

        foreach($methods as $i => $method) /* @var $method ReflectionMethod */
        {
            if($method->getDeclaringClass()->getName() == $class)
                $implementedMethods[] = str_replace('Action', '', $method->getName());
        }

        return array_intersect($implementedMethods, array('get', 'put', 'post', 'delete', 'head', 'options', 'trace'));
    }
}

If you extend this controller for all your actions, you’ll get automated Allow headers every time someone calls a URL that does not implement their request type.

As I said in my talk, be sure to use an op-code cache so reflection doesn’t become your bottleneck!

Also I should mention, this code lives in my ApiController and not in an action helper or similar due to Zend Framework’s performance hit as a result of using magic methods.

Share:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit

Related

Comments

comments

Post navigation

❮ Previous Post: #leedsphp talk slides uploaded
Next Post: Regular Expression (Regex) For Date part extraction/array split ❯

You may also like

PHP
PHP 8.6 clamp Is Tiny but Welcome
August 17, 2026
Programming
MCP Going Stateless Is the Boring Fix It Needed
August 15, 2026
Programming
Docker Compose Start Order Is Not Database Readiness
October 16, 2026
Programming
Soft Delete Still Leaves You With Hard Decisions
November 24, 2026

Leave a Reply Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • AI
  • artificial intelligence
  • Ego-centric
  • Events
  • Films
  • Food
  • Gaming
  • Gym
  • Hardware
  • Holidays
  • News
  • PHP
  • Programming
  • Random Stuff
  • Reviews
  • Science
  • SEO
  • Software
  • Software Engineer
  • Support
  • Uncategorized
  • Work

Categories

  • AI
  • artificial intelligence
  • Ego-centric
  • Events
  • Films
  • Food
  • Gaming
  • Gym
  • Hardware
  • Holidays
  • News
  • PHP
  • Programming
  • Random Stuff
  • Reviews
  • Science
  • SEO
  • Software
  • Software Engineer
  • Support
  • Uncategorized
  • Work

Archives

  • November 2026
  • October 2026
  • August 2026
  • July 2026
  • June 2026
  • May 2026
  • April 2026
  • March 2026
  • August 2023
  • April 2023
  • March 2023
  • April 2016
  • October 2015
  • July 2015
  • June 2015
  • May 2015
  • March 2015
  • February 2015
  • January 2015
  • October 2014
  • December 2013
  • September 2013
  • June 2013
  • October 2012
  • May 2012
  • March 2012
  • February 2012
  • April 2011
  • March 2011
  • October 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009

Copyright © 2026 wade.one.

Theme: Oceanly News Dark by ScriptsTown