Create a production ready rest api with symfony2 (part 1)
- May 23, 2015
What will we cover:
- how to transform a just-started symfony project into a REST-ready project
- how to configure and use the FOSRestBundle to help us in that task
- How to use Doctrine migrations to easily transition your database from version N to version N+1
- how to add functionnal tests to automatically check your API
- how to configure and use the JMSSerializer to automagically serialize and unseralize your data into json (or xml)
- How to plug your API to a Oauth2 service
- mixed in between some generic advices to make your API reality proof
- practical examples of common “tricky things” (Image uploading / pagination / association between resources etc.)
Here we suppose you’re already familiar with what a REST API exactly means, and I really mean the emphasis in exactly
for this I urge you to read these links:
- http://martinfowler.com/articles/richardsonMaturityModel.html
http://blog.steveklabnik.com/posts/2011-07-03-nobody-understands-rest-or-http
www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
An API is made to resist the test of time, so you can really afford to spend one or two hours to carefully read these links.
Example project
All the steps described here are applied in this toy project https://github.com/allan-simon/symfony2-rest-api-example
It’s a Blog-like services with a REST API in json at the end it will permit
- Multiple Authors
- Possiblity to add/edit/delete articles
- Possibility to comments
- Creation of tags to category articles
Things I've learn this week
- May 12, 2015
More than often I find myself trying to remember how did i solve that very one problem I thought I would have to solve only once, so I decided to start writing their solution here:
* How to reload PHP-fpm without restarting it
* How to send the unchanged Host header to proxied server with Nginx
Read More
vagrant with ansible provisionning docker containers for symfony2
- April 12, 2015
In this article we’re going to cover:
- How to use ansible+docker+vagrant to create a one size for all environment for Symfony2 + Nginx + Postgresql + PHP-fpm
- How to transition an existing application to fit in that new environment
This article take the assumption that you have already heard about all the technologies above without being too familiar with them.
As an example will take the existing Oauth2 server I have created: Oauth2Symfony2
Read Moredoctrine2 order by count many to many
- April 9, 2015
Today I found a problem quite common but tricky enough for the solution to share it with you
The problem
I have 2 Entities
- User
- Article
and a “likedByUsers” Many To Many relationship between both
Now I would like to get the Articles ordered by number of ‘likes’ for an API call returning a list of Articles
if you feel smart you can try to find the solution by yourself it’s an interesting exercise about ‘how much do you know doctrine and SQL’
but let me tell you, the solution is like an Egg of Columbus, once you see it it becomes evident, but it was actually not that simple at first.
The solution
Read Moregitlab ci with docker runner
- March 29, 2015
In my company we’re using Gitlab for managing our code and doing code review. For the last months we’ve been standardizing our stack on Symfony/Postgresql/php-fpm. Recently we’ve started automatizing our tests using phpunit. The next step was of course to have a start of continuous integration and to be able to run our tests at each Merge request.
In this article we’re going to view
- How to install gitlab-ci on a separate server than your gitlab instance
- How to put gitlab-ci behind your company frontal server
- How to make gitlab-ci run your tests in a Docker container
symfony create your own third party bundle part 1/4
- March 28, 2015
So you’ve reached the point where you’re reusing the same service accross symfony application, again and again and you feel like this could even be useful for other people. You decide that it will be great if you could do like adults and have your own bundle.
Here is how to create one from scratch, you can find the repo here Oauth2AccessBundle
we cover these steps:
- Creating the base repo in github and composer.json
- Adding the base Classes
- Permit users of your bundle to configure it
- Make your bundle testable
migrate down with doctrine migrations
- March 14, 2015
if you’re using Doctrine in your Symfony2 project, you’re certainly using the excellent Doctrine Migration Bundle but you may have seen that documentation is not staging clearly how to migrate down
for that:
php app/console doctrine:migrations:status
take the number in “Current Version” (format YYYYMMDDHHMMSS)
and then run
php app/console doctrine:migrations:execute YYYYMMDDHHMMSS --down
that’s all folks !
Read Moresymfony 2.6 tips #1
- March 11, 2015
Here a list of tips for symfony 2.6 I’ve gathered these days
- how to skip test in phpunit
- how to disable form validation in test
- (Doctrine) don’t use findById() when you just mean find()
Redmine-cli and redmine API
- February 25, 2015
I found a tool to manage redmine from the cli, in python:
pip install redmine-cli
then a config file ~/.redmine-cli
[default]
key=API_KEY_YOU_CAN_SEE_IN_YOUR_ACCOUNT_ON_REDMINE
my_id=ID_YOU_CAN_ON_YOUR_PROFILE_PAGE
root_url=HOME_PAGE_OF_REDMINE
then you can do
redmine open 123
to open the issue 123 in your browserredmine issues
to see your issuesredmine issue 123
to see information about issue 123
(it has more options I let you check the doc)
(hopefully my modification are in PR on the original repo and will be soon integrated) my modification include so far:
redmine issue 123 long
to see more detailled information about the issueredmine update 123 'in progress'
to update the status toin progress
(admitting your redmine has this status)
Form name goes into global scope in javascript
- February 23, 2015
An interesting problem a friend of mine discovered today
Can you spot the problem?
Read More