Gotcha: Upgrading To FOSRestBundle 2.0

This is not a huge issue, and likely quite easy to identify and fix. But I’m the kind of person who hits Google before engaging brain, and I didn’t find a direct answer (boo, wake up brain!).

I have, this very evening, decided to try FOSRestBundle 2.0. It’s currently still in development, so isn’t available without a little naughty composer tagging:

    "require": {
        "friendsofsymfony/rest-bundle": "^2.0@dev"
    },

A little tip there – if you use the @dev after your requested version number, you can force through development packages without globally changing your project’s minimum-stability  setting.

Anyway, being that I’ve been coding for the last 12.5 hours, I went with the lazy man’s option of copy / pasting my config from a FOSRestBundle 1.7 installation I happened to have laying around.

fos_rest:
    view:
        exception_wrapper_handler:  null
        mime_types:
            json: ['application/json', 'application/x-json']
            jpg: 'image/jpeg'
            png: 'image/png'

Firstly, I got this:

InvalidConfigurationException in ArrayNode.php line 317:
Unrecognized option "exception_wrapper_handler" under "fos_rest.view"

Which confuses me, as this seems to be the same as what’s in the configuration reference.

Anyway, simply removing that line makes the error go away.

The next error I got was this:

Warning: array_merge(): Argument #1 is not an array
500 Internal Server Error - ContextErrorException

And what this turns out to be is that the mime_types must now all be arrays:

fos_rest:
    view:
        mime_types:
            json: ['application/json', 'application/x-json']
            jpg: ['image/jpeg']
            png: ['image/png']

So there you go 🙂

Custom Route Name in FOSRESTBundle

I recently had an issue where I wanted to have a controller called SocialMediaProfilesController, but whatever I tried, the automatically generated route names kept coming out as e.g. get_socialmedia_profiles.

I haven’t dived deep into the code to determine as to why this weird spacing issue is occurring, but my guess would be that if you created a controller called SomeEvenLongerStringController, your action might be get_someevenlonger_string. But as I say, I haven’t tried this, so it’s just a guess as to what might be happening.

Anyway, the fix is pretty simple. It’s probably out there on Google, or in the docs, or something, but I couldn’t find it. Instead, I stumbled upon this and it works, so here we go:

<?php

namespace AppBundle\Controller;

use FOS\RestBundle\View\View;
use FOS\RestBundle\Controller\Annotations;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Routing\ClassResourceInterface;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
// some other use statements here, but these are the interesting ones 

/**
 * Class SocialMediaProfilesController
 * @package AppBundle\Controller
 * @Annotations\RouteResource("socialmediaprofiles")
 */
class SocialMediaProfilesController extends FOSRestController implements ClassResourceInterface
{

By using the RouteResource annotation, you can control how the route names will be generated.

Without the annotation:

  cget_accounts_socialmedia_profiles     GET      ANY      ANY    /accounts/{accountId}/social-media-profiles
  get_accounts_socialmedia_profiles      GET      ANY      ANY    /accounts/{accountId}/social-media-profiles/{socialMediaProfileId}

And then with:

  cget_accounts_socialmediaprofiles     GET      ANY      ANY    /accounts/{accountId}/social-media-profiles
  get_accounts_socialmediaprofiles      GET      ANY      ANY    /accounts/{accountId}/social-media-profiles/{socialMediaProfileId}

So that’s pretty handy.