Working with AngularJS and Silex as Resource provider

This days I’m playing with AngularJS. Angular is a great framework when we’re building complex front-end applications with JavaScript. And the best part is that it’s very simple to understand (and I like simple things indeed). Today we are going to play with Resources. Resources are great when we need to use RestFull resources from the server. In this example we’re going to use Silex in the backend. Let’s start.

First of all we must realize that resources aren’t included in the main AngularJS js file and we need to include angular-resource.js (it comes with Angular package). We don’t really need resources. We can create our http services with AngularJS without using this extra js file but it provides a very clean abstraction (at least for me) and I like it.

    <script src=""></script>
    <script src=""></script>

We’re going to create a simple application with CRUD operations in the table. In the example we will use one simple SqlLite database (included in the github repository)

CREATE TABLE main.messages (
  message VARCHAR NOT NULL );

Our main (and only one) html file:

<!DOCTYPE html>
<html ng-app="MessageService">
    <title>Angular Resource Example</title>
    <script src=""></script>
    <script src=""></script>
    <script src="js/services.js"></script>
    <script src="js/controllers.js"></script>
<body ng-controller="MessageController">

<h2>Message list <a href="#" ng-click="refresh()">refresh</a></h2>
    <li ng-repeat="message in messages">
        <a href="#" ng-click="deleteMessage($index,">delete</a>
        #{{}} - {{}} - {{message.message}}
        <a href="#" ng-click="selectMessage($index)">edit</a>

    <input type="text" ng-model="author" placeholder="author">
    <input type="text" ng-model="message" placeholder="message">

    <button ng-click="add()" ng-show='addMode'>Create</button>
    <button ng-click="update()" ng-hide='addMode'>Update</button>
    <button ng-click="cancel()" ng-hide='addMode'>Cancel</button>

As we can see we will use ng-app=”MessageService” defined within the js/services.js file:

angular.module('MessageService', ['ngResource']).factory('Message', ['$resource', function ($resource) {
    return $resource('/api/message/resource/:id');

And our controller in js/controllers.js:

function MessageController($scope, Message) {

    var currentResource;
    var resetForm = function () {
        $scope.addMode = true;
        $ = undefined;
        $scope.message = undefined;
        $scope.selectedIndex = undefined;

    $scope.messages = Message.query();
    $scope.addMode = true;

    $scope.add = function () {
        var key = {};
        var value = {author: $, message: $scope.message}, value, function (data) {

    $scope.update = function () {
        var key = {id:};
        var value = {author: $, message: $scope.message}, value, function (data) {
            currentResource.message = data.message;

    $scope.refresh = function () {
        $scope.messages = Message.query();

    $scope.deleteMessage = function (index, id) {
        Message.delete({id: id}, function () {
            $scope.messages.splice(index, 1);

    $scope.selectMessage = function (index) {
        currentResource = $scope.messages[index];
        $scope.addMode = false;
        $ =;
        $scope.message = currentResource.message;

    $scope.cancel = function () {

Now the backend part. As we said before we will use Silex. We’re going to use also RouteCollections to define our routes (you can read about it here). So our Silex application will be:

require_once __DIR__ . '/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RouteCollection;
use Silex\Application;

$app = new Silex\Application();

$app['routes'] = $app->extend('routes', function (RouteCollection $routes, Application $app) {
        $loader     = new YamlFileLoader(new FileLocator(__DIR__ . '/config'));
        $collection = $loader->load('routes.yml');

        return $routes;

    new Silex\Provider\DoctrineServiceProvider(),
        'db.options' => array(
            'driver' => 'pdo_sqlite',
            'path'   => __DIR__ . '/db/app.db.sqlite',


We define our routes in the messageResource.yml

  path: /resource
  defaults: { _controller: 'Message\MessageController::getAllAction' }
  methods:  [GET]

  path: /resource/{id}
  defaults: { _controller: 'Message\MessageController::getOneAction' }
  methods:  [GET]

  path: /resource/{id}
  defaults: { _controller: 'Message\MessageController::deleteOneAction' }
  methods:  [DELETE]

  path: /resource
  defaults: { _controller: 'Message\MessageController::addOneAction' }
  methods:  [POST]

  path: /resource/{id}
  defaults: { _controller: 'Message\MessageController::editOneAction' }
  methods:  [POST]

And finally our Resource controller:

namespace Message;

use Silex\Application;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class MessageController
    public function getAllAction(Application $app)
        return new JsonResponse($app['db']->fetchAll("SELECT * FROM messages"));

    public function getOneAction($id, Application $app)
        return new JsonResponse($app['db']
            ->fetchAssoc("SELECT * FROM messages WHERE id=:ID", ['ID' => $id]));

    public function deleteOneAction($id, Application $app)
        return $app['db']->delete('messages', ['ID' => $id]);

    public function addOneAction(Application $app, Request $request)
        $payload = json_decode($request->getContent());;

        $newResource = [
            'id'      => (integer)$app['db']
                ->fetchColumn("SELECT max(id) FROM messages") + 1,
            'author'  => $payload->author,
            'message' => $payload->message,
        $app['db']->insert('messages', $newResource);

        return new JsonResponse($newResource);

    public function editOneAction($id, Application $app, Request $request)
        $payload = json_decode($request->getContent());;
        $resource = [
            'author'  => $payload->author,
            'message' => $payload->message,
        $app['db']->update('messages', $resource, ['id' => $id]);

        return new JsonResponse($resource);

And that’s all. Our prototype is working with AngularJS and Silex as REST provider. We must take care about one thing. Silex and AngularJS aren’t agree in one thing about REST services. AngularJS removes the trailing slash in some cases. Silex (and Symfony) returns HTTP 302 moved temporaly when we’re trying to access to the resource without the trailing slash but when we’re working with mounted controllers we will obtain a 404 page not found (bug/feature?). That’s because my REST service is /api/message/resource/:id instead of /api/message/:id. If I chose the second one, when angular tries to create a new resource, it will POST /api/message instead of POST /api/message/. We’re using mounted routes in this example:

  prefix: /message
  resource: messageResource.yml

With one simple Silex application (without mounted routes) in one file it doesn’t happen (we will see HTTP 302 and a new request with the trailing slash). Because of that I use this small hack to bypass the problem.

You can see the full code of the example in my github account

About Gonzalo Ayuso

Web Architect. PHP, Python, Node, Angular, ionic, PostgreSQL, Linux, ... Always learning.

Posted on June 3, 2013, in AngularJS, php, silex, Symfony, Symfony2, Technology and tagged , , , , . Bookmark the permalink. 11 Comments.

  1. Reblogged this on clasense4 blog and commented:

  2. Hello Gonzalo! this error:
    FatalErrorException: Error: Cannot use object of type Acme\TaskBundle\Entity\Task as array in /var/www/Symfony/src/Acme/TaskBundle/Controller/DefaultController.php line 31

    code generate error:

    return $this->redirect($this->generateUrl(‘acme_hello_homepage’,array(‘name’=>$form->get(‘task’)->getData())));

  3. ivangarzoncruz

    Hey Gonzalo nice tutorial very useful. I’m trying to link another template when I finish a validation in the controller but I couldn’t do that, Could you give me a hand with any suggestion?
    In service.js I’m trying this but it is not working:

    angular.module(‘LoginService’, ['ngResource', 'ngRoute'])
    .factory(‘Login’, ['$resource', function ($resource) {
    return $resource('/api/user/login/:email', { email: '@email' }, {
    query: { method: 'GET', isArray: false }
    * Route
    .config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/login/:email', {
    controller: 'loginUser',
    templateUrl: 'views/matches.twig.html'
    .otherwise({ redirectTo: '/login' });

    But I don’t know if I am on the right track.

    • Maybe I didn’t understand your question but it looks that there’s something wrong. Angular and Silex are different things. templateUrl doesn’t use twig. And Controller ‘loginUser’ isn’t’ defined within your angular application. Maybe that’s the root of your problems

  4. Great, thanks I was just implementing angularjs on an old sillex project and wasn’t getting the right way to get the json data from the view! thanks!

  1. Pingback: Working with jQuery and Silex as RestFull Resource provider | Gonzalo Ayuso | Web Architect

  2. Pingback: Trabalhe com JQuery e Silex como recurso fornecedor de RestFull | iMasters

  3. Pingback: Enabling CORS in a RESTFull Silex server, working with a phonegap/cordova application | Gonzalo Ayuso | Web Architect

  4. Pingback: Working with AngularJS and Silex as Resource provider - Angular News

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: