Thursday, August 21, 2014

Let's keep it simple. It is just a quick draft how can you set up a Symfony2 development environment in a few minutes.I'll use Vagrant (you'll need version 1.5 at least) and my dev box to create the desired environment.The box main components are: Ubuntu14.04, Nginx1.6, Php5.5Fpm, Xdebug, Composer, MySql5.5, NodeJs0.10.30, Redis and it has PhpMyAdmin, Webgrind and RedisCommander.So the steps are:

Wednesday, July 9, 2014

If you have ever have a problem with php forking, upstart will be your good friend.What is upstart?
In short: service management daemon

"Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running."

Let's make it clear (from our perspective):
You can start any of your (php) scripts as a daemon (even with arguments) with a handy toolset: status / start / stop / restart

Config your job (e.g. a script)
Pretend that you have a my-awesome-app.php, which you want to start as daemon on every system boot. Your script can easily become fragile, so you also want to monitor it with a respawn mechanism.

First, you have to create an upstart config and place it in the /etc/init directory. In this case it will be this one: /etc/init/my-awesome-app.conf

Friday, July 4, 2014

I have a Digital Ocean Vps, but only for hobby projects. So it has merely 512Mb memory. If you wantto run multiple applications on a small development server you can operate with more memory, just use swap files."Swap
space will allow the operating system to temporarily move data from
memory onto the disk when the amount of information in memory exceeds
the physical memory space available"Of course it will be much slower. Fortunately D.Ocean has only Ssd hard drives, so Ssd improves the situation.Here are the bash lines from a Digital Ocean article:sudo fallocate -l 1G /swapfile; sudo mkswap /swapfile; sudo swapon /swapfileCreate an empty 1Gb file; Format to swap space; Enable itThis swap file remains until the next reboot!

Sunday, June 1, 2014

After Act I. we know the basics of the REST architectural style. Before start the development process we should take a look at an important Model, which is a kinda de-facto to measure how RESTful is an API.That is the Richardson Maturity Model. This model also helps us to see a big picture of how should look our API in a way of RESTful.Let's take a look at Martin Fowler's explanation.

REST has a Uniform Interface, which does not have to be the HTTP, but we're web developers so we'll use HTTP as our Uniform Interface!Level 0 - The Swamp of POX (Plain Old Xml):At Level 0 we use HTTP as a tunnel to Remote Interactions. We use only one entry point (URI) and one kind of HTTP method (e.g. POST). Examples: SOAP, XML-RPC, JSON-RPCLevel 1 - Resources:Now we use multiple entry points (URIs), which are unique identifiers to retrieve resources. This level still uses only one HTTP method. Examples: http://api.example.com/cars, http://api.example.com/cars/1, http://api.example.com/cars/2Level 2 - HTTP Verbs:At Level 2 we proudly introduce multiple HTTP verbs. Use GET for read a resource, DELETE to delete it and so on (anyway REST != CRUD).Another important principle is that the usage of the proper HTTP response codes to indicate status. Do not rely always the response 200 (OK), we should be more sophisticated and should toss our almost RESTful API toward a semantic web.Examples: After a resource creation the proper HTTP response code is the 201 (Created)Level 3 - Hypermedia Controls:Hypertext As The Engine Of Application State (HATEOAS).This is the highest level. Without reaching this level our API is not RESTful!Hypermedia controls are URIs attached to a given resource. These controls tell us what can we able to do next with the resource (e.g. add a related resource to it). One of the main benefit is that it allows us to change the URI schemes without breaking the clients. Another one is that it helps to explore the API.Examples: go to Martin Fowler's explanationReferences:http://nirmata.com/2013/10/rest-apis-part-1/http://martinfowler.com/articles/richardsonMaturityModel.html

Thursday, May 22, 2014

To understand what is a RESTful API, first we have to clear what is REST.(this post is far away from a full review of REST, just a quick one to help building a RESTful API, you can find deeper reviews among the references [at the bottom])REST = REpresentational State TransferThe elaboration of REST is a part of Roy Fielding's DissertationSo, what is REST?

Architectural style for distributed hypermedia systems such as World Wide Web

So, REST is an architectural style. Not identical to HTTP, not an API and it's not just one and only for APIs. It is a set of constraints to build a simplified and decoupled system such as Web.But we want to create an API in REST style, so we'll use the REST constraints to achieve our goal!Before move on the architectural constraints, first we should take a quick look at the architectural elements.Architectural elements:(just a minimal representation)

Data elements

"The key aspect of REST is the state of the data elements, its components communicate by transferring representations of the current or desired state of data elements"

Resources

"Any
information that can be named can be a resource: a document or image, a
temporal service (e.g. "today's weather in Los Angeles"), a collection
of other resources, a non-virtual object (e.g. a person), and so on"

Representations

"It is something that is sent back and forth between clients and servers"

"Never send or receive resources, only their representations"

"Sequence of bytes, HTML document, archive document, image document"

Part (it can be the entire) of the resource state

Architectural constraints:

Uniform interface

Defines the Interface between Client and Server. Simplifies and Decouples the Architecture. Uniform interface has 4 constraints:

Identification of Resources

"Resources must have a unique identifier which can be use to retrieve a resource (e.g. URI)"

Manipulation of Resources through Representations

"Resources can be updated (or added) by sending representations from the client to the server"

Self-descriptive messages

"It means each message contains all the information necessary to complete the task"

Hypermedia As The Engine Of Application State

HATEOAS

"The ability to navigate resources through hyperlinks/hypermedia is the hallmark of REST"

Layered system

A client cannot rely a direct connection to the end server

There can be intermediary layers between the client and the end server

An intermediary can be a load-balancer or proxy...

Client-Server

Separation of concerns

Improves client portability

Server can be simpler, more scalable

Stateless

"each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server"

Cacheable

"the data within a response to a request be implicitly or explicitly labeled as cacheable or non-cacheable"

Code on demand (optional)

"server can extend the client functionaility by sending back scripts or code"

The code is the know-how to process the representation of the given resource

Monday, May 12, 2014

Fixtures are used to load a controlled set of data into a database. This data can be used for testing or could be the initial data required for the application to run smoothly (from symfony.com)

So, the DataFixture Bundle is a great way to help initialize your application, e.g. set up an admin user. You can use it all through the development phase continuously, it has an append option. Without the append argument your database will be erased before the fixtures get injected.If you plan to use this bundle for testing, you can mix it up with the Faker library to create various test data.When you work on a not so large project it's a good enough bundle to solve your problems. But if the project is a large one and you want to use a more sophisticated way to make fixtures you should use the Alice library! (it has a Bundle for Symfony2 -> AliceBundle)Alice calls itself an Expressive fixtures generator. It has a lot of handy tools in its toolbox.You can create fixtures in a config file! Let's see a Static one: