Silex Routing vs Trailing Slashes

In Silex-based projects I always define my routes with a trailing /. When defining a route with a trailing slash, the router will respond to both the route without and with slash:

  • $app->get('/hello', …); will respond to http://localhost/hello but not to http://localhost/hello/
  • $app->get('/hello/', …); will respond to http://localhost/hello and to http://localhost/hello/

Unfortunately this only works for GET routes. To make other methods work graze/silex-trailing-slash-handler comes in handy:

Handle requests missing a trailing slash in Silex by appending a slash and issuing an internal sub-request.

Usage is really simple, as graze/silex-trailing-slash-handler comes with a service provider that takes care of everything.

$app->get('/', function () {
    return 'Hello World!';
})

$provider = new \Graze\Silex\ControllerProvider\TrailingSlashControllerProvider();
$app->register($provider);
$app->mount('/', $provider);

Just remember to define your routes first, before mounting the controller provider.

graze/silex-trailing-slash-handler

Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

☕️ Buy me a Coffee ($3)

Using ConsoleServiceProvider with Silex

use Knp\Provider\ConsoleServiceProvider;

$app->register(new ConsoleServiceProvider(), array(
	'console.name' => 'ConsoleApp',
	'console.version' => '1.0.0',
	'console.project_directory' => __DIR__ . '/..'
));

Good writeup and code by Ken, a former student of mine.

Using ConsoleServiceProvider with Silex →

Want to start developing with Silex? Then check out my freely available course materials on the subject.

Silex Code Fiddles/Examples

silex

For the course Webscripting2 I’m lecturing at ikdoeict.be/KaHo Sint-Lieven, we’re working with Silex. Next to the slidedeck explaining Silex, all examples are freely available on GitHub (links at the bottom of this post)

The collection of examples start with the very basics of Silex and introduce more routing options and services as they continue.

  1. Basic:
    The basic Silex app structure.
  2. Hello:
    The infamous Hello world showcasing the basic Silex app structure, dynamic routing, redirects, and error handlers.
  3. Olleh:
    More routing showcasing requirements, default values, and Route Variable Converters.
  4. Tweets:
    Practical example demonstrating routes using some (static) data.
  5. Tweets Organized:
    Organizes the controllers for the different routes in classes; it’s all about the structure!
  6. Tweets Twig:
    Introduces the built-in TwigServiceProvider, allowing one to use Twig within a Silex app.
  7. Doctrine:
    Introduces the built-in DoctrineServiceProvider, allowing one to use Doctrine’s DBAL within a Silex app.
  8. Doctrine Organized:
    Organizes our codebase a tad better: per entity we introduce a repository which acts as a database layer. Depends on the custom Service Provider knplabs/repository-service-provider (loaded via Composer).
  9. Static Pages:
    Introduces named routes, the UrlGeneratorServiceProvider and a neat little trick to quickly add a few static pages to a project
  10. Middlewares:
    Introduces Middlewares and along with that the MonologServiceProvider
  11. Forms & Validation:
    Introduces the FormServiceProvider and ValidatorServiceProvider

More examples are still in development. The examples are best studied along with the accompanying slides.

Webscripting2 – Serverside Scripting – Silex Code Fiddles →
Webscripting2 – Serverside Scripting – Course Materials →

Note: the course materials are best cloned locally. The fiddles are also included in that repository. Be sure to composer install all examples.

Silex $app['autoloader']->registerNamespace() deprecated

The past few days I’ve been playing around with Silex, a micro PHP Framework. At a certain point I got stuck in the process when using a custom controller: the darn class just wouldn’t load and the (otherwise excellent) documentation on the Silex site has not mention on how to load it.

Most of the information one finds on the internet instruct you to do this (line #3):

// source for /app/bootstrap.php
require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$app['autoloader']->registerNamespace('Bramus', __DIR__. '/src');

That information however, is deprecated and won’t work.

A working solution I eventually found was this one:

// source for /app/bootstrap.php
$loader = require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$loader->add('Bramus', __DIR__. '/../src');

But that code just stinks I must say, it just doesn’t feel right.

Turns out the nicest solution is the most simple one: just register your custom namespace in composer.json. For example:

{
	"require": {
		"silex/silex": "1.0.*@dev"
	},
	"autoload": {
		"psr-0": {
			"Bramus": "src/"
		}
	}
}

After changing it, run a composer update and you’re good to go.

Hope this helped you, struggled with it myself quite some time.