Created
January 17, 2011 17:44
-
-
Save wilmoore/783143 to your computer and use it in GitHub Desktop.
Using Traits instead of inheritance to build a Restful Dispatcher in ZF2 MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// REF: http://framework.zend.com/wiki/display/ZFDEV2/Proposal+for+MVC+Interfaces | |
trait RestDispatcher | |
{ | |
public function dispatch(Request $request, Response $response = null) | |
{ | |
if (!$request instanceof HttpRequest) { | |
throw new DomainException('REST expects HTTP requests'); | |
} | |
$resource = $this->getResource(); | |
switch (strtoupper($request->getMethod())) { | |
case 'GET': | |
// ... | |
break; | |
case 'POST': | |
// ... | |
break; | |
case 'PUT': | |
// ... | |
break; | |
case 'DELETE': | |
// ... | |
break; | |
case 'HEAD': | |
// ... | |
break; | |
default: | |
$response->getStatus()->setStatusCode(405, 'Method Not Allowed'); | |
} | |
return $response; | |
} | |
} | |
// In a controller file: | |
namespace Blog\Controller; | |
class Entry implements Dispatchable | |
{ | |
use RestDispatcher; | |
public function getResource() | |
{ | |
return new Blog\Model\EntryResource; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment