Importing Routes from a Controller

This is how you import routes from a specific controller (only works if you used annotations):

whatever_routes:
    resource: "@MyBundle/Controller/WhateverController.php"
    type:     annotation

This is very useful if you have multiple controllers in single bundle but you can’t import the whole Controller directory at once because some routes have to be treated in a special way, e.g. you want a different prefix for every controller.

whatever_routes:
    resource: "@MyBundle/Controller/WhateverController.php"
    prefix:   /some-bundle/whatever
    type:     annotation

other_routes:
    resource: "@MyBundle/Controller/AnotherController.php"
    prefix:   /some-bundle/another
    type:     annotation

For reference, this is the original documentation of that feature.

PHPUnit: contains vs. stringContains

Note to myself: If I ever see that error again when running PHPUnit tests

Invalid argument supplied for foreach()

phar://C:/Program Files (x86)/PHPUnit/phpunit.phar/PHPUnit/Framework/Constraint/TraversableContains.php:110
phar://C:/Program Files (x86)/PHPUnit/phpunit.phar/PHPUnit/Framework/Constraint.php:82
phar://C:/Program Files (x86)/PHPUnit/phpunit.phar/PHPUnit/Framework/Constraint/And.php:113

please remember that $this->contains() is not the same as $this->stringContains(). The first one is a constraint for arrays, the second one is for strings.

SonataNotificationBundle does not consume messages

Today I was playing around with SonataNotificationBundle. I simply followed the installation instructions, but I encountered some problems.

First problem: You have to manually add SonataEasyExtendsBundle as a dependency. For some reason it is not automatically added by Composer. I guess they’ve just forgot it in the composer.json, so maybe this problem is already fixed by the time you read this.

After the bundle was working I’ve implemented a new component to dispatch some messages. Everything was working fine, I could even see the message in the database (used Doctrine for the message queue). To complete that test scenario I implemented a consumer for my message type and registered it as a tagged service. I started the consumer job as described in the documentation …

app/console sonata:notification:start --env=prod --iteration=250

… but nothing happend!

Second problem: The messages did not process. Some debugging in the code showed me that it only fetched messages with type default from the database. This was strange, because the console told me that my message consumer is registered and there was no default type at all. I did some research on this problem and found a very useful post of someone having the exact same problem. There was as a working configuration in the thread so a gave it a try – and it worked! The trick was to configure at least one queue. Your configuration should look similar to this:

sonata_notification:
    queues:
        - { queue: catchall, default: true }

The catchall is just a name for the queue, it can be anything. It’s more important to make it the default queue, which is done with default: true.