RESTful API and versioning in Zend Framework 2 - php

I created a RESTful API in Zend Framework 2.1 but I still don't know what is the best approach to make versioning for this API. I also use the doctrine as a model part of my app. I'd like to have links like that:
api.name-of-my-project.com/v1/products
api.name-of-my-project.com/v1/products/123
where v1 describes the version of API.
So far I already created API without versioning and I also have some ideas about how to create versioning but first I'd like to know your opinions about this problem. What should I do to separate controllers and maybe other classes from different versions and how to manage the routing? By the way I think that I am not really interested about creating another module because I already have module like API.
The final app should be able to work with "many" versions of API. I'd like to also inherit some functionality from older version of API in my new one.

You can do this with zf2 routing array in your module config
Map routes with v1 to versiononecontroller and v2 to versiontwocontroller

Related

Best way to build RESTful services using Zend Framework 3

We are starting a new project that will make use of RESTful services to talk to web clients (AngularJS) and mobile platforms (Android and iOS).
The idea is to hide the bussiness logic inside the RESTful services and to use the same code for both web client and mobile platforms.
Our server is being built in PHP using Zend Framework 3 (I´m new to the framework). The application will be have dozen of entities with hundreds of services.
I´ve noted on the web that is seems that ZF2 has a REST module, but I can´t find one for ZF3 (at least not in the documentation).
Some questions I have:
What is the best path to provide RESTful services using ZF3 ?
Shall I install a ZF module ? Is there something ready to be used that comes with ZF3 ?
Shall I create a Module in the code just for my services ? Shall the other
modules call this REST module for business logic ?
ZF3 is still shipped with an AbstractRestController, which can be found here. Apigility could be a starting point for your REST application, but you could also consider implementing only some parts of Apigility and build your own logic on top of that. Interesting modules can be found in the ZF-Campus GitHUB repository:
ZF-Rest
ZF-Content-Negotiation
ZF-Content-Validation
ZF-Hal (depending on whether HAL-Json works for you...)
Most (if not all) of those modules are refactored to support ZF3 (and backwards compatible with ZF2).
Browse through the repository yourself because you might find additional useful modules.

Getting a list of REST resources from Slim

I'm using the PHP Slim Framework for my RESTful project.
I was wondering if its possible to get a list of all the callback registered with Slim somehow so as to have a list of all the REST resources that are available for the app?
I assume this will involve some sort of pre-compile but I'm not sure how to do it.
I haven't used the Slim Framework yet (looks quite exciting, thanks for the pointer), but my best guess would be using PHPDocumentor to generate documentation about your rest endpoints.
You can find it at the phpdocumentor website

Symfony project using CURL - building project with API

I have never used a PHP framework before and am considering using Symfony for my next one. However, I have already built an API which has all my main classes. I will therefore connect to this API using CURL. Can anyone advise on where to start with such a project. I am working through a Symfony tutorial but none of it seems to apply to what I need to do as I do not need to create database connections or classes as I will be building this project on my existing API.
If you don't need a lot of what you see in the Symfony tutorials you might be able to get away with Silex. Silex is a mini-framework with alot of the core aspects of Symfony, but without the extra stuff.
http://silex.sensiolabs.org/

Luracast Restler in CodeIgniter

I am trying to put in Restler as part of my CI Library. I wonder if this is possible with Restler because I'm getting Server Error. Should I just separate Restler folder to my CI folder (which works right now)?
I want to integrate it to my CI so I can access the models e.g for authentication, getting user data...etc. instead of creating another config file and model classes.
I have invested a lot of time trying to find some workaround to make Restler work with CI but no luck.
Thanks!
I don't think there's an easy way to integrate Restler into CI, you would have to totally rework the CI routing class.
I would recommend that you instead go with Phil Sturgeon's excellent REST server for CI, which is a pretty simple to put into your existing CI application.

Using Symfony doctrine models in a REST framework

I have a web application which has been developed with symfony 1.4. I have a pretty large code base (and growing). Circa 80,000 lines of code (actions, forms, models, templates etc.)
I'm using the default doctrine version which ships with symfony 1.4.
I've just started developing a mobile version using Sencha touch. I don't wish to use symfony for the REST web services because:
REST services in Symfony 1.4 is not great. For example, If i want a PUT request I have to pass a 'sf_method' parameter specifiying that the request method is PUT. This isn't true REST and it's not ideal for Sencha touch.
I don't need all of the unnecessary symfony functionality(for example the plugins that are autoloaded in the ProjectConfiguration file, the form framework etc.) that you'd use to develop a standard web app. All I need is to define my REST routes and return the specified JSON (as everything that needs to be returned for Sencha touch will be JSON)
I want to keep my mobile app as bloated-free, efficient and quick as possible. And unfortunately for this task, Symfony 1.4 would not be the best choice for using as the backend architecture for my mobile app. If I had chosen symfony2 (it was in it's beta phase, alas) it would be a different story as symfony2 supports true REST functionality. What I do need, however, is the ability to use my current doctrine models (I have circa 90 models) in a chosen REST framework.
Basically, in a nutshell what I need is as simple as this:
Call a rest route->Query my doctrine models->return the JSON without using symfony.
So my question, what would be your advice? I don't want this to be a question of which is the best PHP rest framework, however, what I would like to know is what would be a good REST framework which i can develop efficiently and quickly REST service, make use of my doctrine models and is easily extendable.
Here at my employer, I've created a rather big application with a ExtJS frontend, and Symfony 1.4 backend. And two be honest, I don't feel limited by Symfony 1.4 in any way?
First of: I created my own base controller class (which extends sfActions). This controller can handle (render) different types of data. It has generic handling for Doctrine_Query, Doctrine_Collection, Doctrine_Model and array types.
Also the plugins make me help organize the code, and in some cases plugins are shared between differend projects, so that's also a big plus.
And the extra functionality like forms: it's only prepared for you in the autoloader, you don't have to use it. And I don't think it causes any real performance issues (at least not for me). But I like to use the extra sfValidator framework, to make sure data are correct.
The only real "problem" is indeed the HTTP REST-ful commands, especially PUT and DELETE. I just worked around this problem by generating a controller for each 'manageable' model, and implement specific get, list, create, update and delete actions. So when I would like to manage an Object, I call the objects controller, which has executeCreate, executeUpdate and executeDelete actions.
The reason I read, was that Symfony didn't and couldn't implement this feature because PHP has really bad support this. I don't know if this is true, but if this is your only 'real' issue, you could try to fix this in the Symfony core.
So my advice:
If the raw performance is your problem: try profiling your code, install a opcode (APC) cache, and profile your code (yes, that's double).
If the HTTP PUT command is your problem: I would either work around this (that's the way I solved it), or try to fix it in the core.

Categories