background preloader

ZF2

Facebook Twitter

RESTful APIs with ZF2, Part 2phly. RESTful APIs with ZF2, Part 2 In my last post, I covered some background on REST and the Richardson Maturity Model, and some emerging standards around hypermedia APIs in JSON; in particular, I outlined aspects of Hypermedia Application Language (HAL), and how it can be used to define a generic structure for JSON resources. In this post, I cover an aspect of RESTful APIs that's often overlooked: reporting problems. Background APIs are useful when they're working. But when they fail, they're only useful if they provide us with meaningful information; if all I get is a status code, and no indication of what caused the issue, or where I might look for more information, I get frustrated. In consuming APIs, I've come to the following conclusions: Error conditions need to provide detailed information as to what went wrong, and what steps I may be able to take next.

Why Status Codes Aren't Enough A status code simply isn't enough information most of the time. Custom Media Types API-Problem vnd.error. RESTful APIs with ZF2, Part 3phly. RESTful APIs with ZF2, Part 3 In my previous posts, I covered basics of JSON hypermedia APIs using Hypermedia Application Language (HAL), and methods for reporting errors, including API-Problem and vnd.error. In this post, I'll be covering documenting your API -- techniques you can use to indicate what HTTP operations are allowed, as well as convey the full documentation on what endpoints are available, what they accept, and what you can expect them to return. While I will continue covering general aspects of RESTful APIs in this post, I will also finally introduce several ZF2-specific techniques. Why Document? If you're asking this question, you've either never consumed software, or your software is perfect and self-documenting. In the case of APIs, those consuming the API need to know how to use it.

What endpoints are available? Where Should Documentation Live? This is the much bigger question. The Allow header details the allowed HTTP methods for the given resource. End-User Documentation. RESTful APIs with ZF2, Part 1phly, boy, phly. RESTful APIs with ZF2, Part 1 RESTful APIs have been an interest of mine for a couple of years, but due to circumstances, I've not had much chance to work with them in any meaningful fashion until recently.

Rob Allen and I proposed a workshop for PHP Benelux 2013 covering RESTful APIs with ZF2. When it was accepted, it gave me the perfect opportunity to dive in and start putting the various pieces together. Background I've attended any number of conference sessions on API design, read countless articles, and engaged in quite a number of conversations. JSON is fast becoming the preferred exchange format due to the ease with which it de/serializes in almost every language. Richardson Maturity Model As a quick review, the Richardson Maturity Model has the following 4 levels: Level 0: "The swamp of POX. " A few years ago, I started seeing a trend towards Level 2. I'd argue that the majority of web developers are quite happy to be at Level 2 -- and have no problem staying there. HAL Media Types. Doctrine 2 ORM and Zend Framework 2 tutorial.

Note: these slides are combined with a webcast that you can find on this page . Marco Pivetta Doctrine core team Zf2 contributor Modules developer time waster @Ocramius - Ocramius Main Libraries BjyAuthorize, AssetManager, ZeffMu, ZfrRest, OcraDiCompiler, OcraServiceManager, OcraCachedViewResolver, DoctrineModule, DoctrineORMModule, DoctrineMongoODMModule, VersionEyeModule Doctrine Project An incubator for persistence-oriented libraries What is Doctrine ORM? Doctrine ORM is an Object Relational Mapper It is inspired by Hibernate and the JPA (JSR-317) It is based on a DBAL (DataBase Abstraction Layer) Allows developers to save and load POPO with SQL An ORM gives you the impression that you are working with a "virtual" database (graph) composed by objects Simply put: Forget the database!

The Modules! DoctrineModule basic common functionality DoctrineORMModule ORM/SQL Connection DoctrineMongoODMModule ODM/MongoDB Connection Installation! Php composer.phar require doctrine/doctrine-orm-module:0.7 . . Test it! Getting Started with REST and Zend Framework 2. Today i want to show you how to build a rest application. This tutorials assume you have completed the Getting Started. I will be repeating lot of the steps allready explained in there. There is also a sample Album module which you can install from here. Start by creating a directory called AlbumRest under module with the following subdirectories to hold the module’s files: zf2-tutorial/ /module /AlbumRest /config /src /AlbumRest /Controller /test Create Module.php in the AlbumRest module at zf2-tutorial/module/AlbumRest: <?

Create a file called module.config.php under zf2-tutorial/module/AlbumRest/config: <? As we are in development, we don’t need to load files via the classmap, so we provide an empty array for the classmap autoloader. <? We now need to tell the ModuleManager that this new module exists. (Changes required are highlighted using comments.) <? As you can see, we have added our AlbumRest module into the list of modules after the Album module. <? <? <? <? You should also add: Get Album.