Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up case insensitive routes with Slim framework?

Tags:

php

routes

slim

I have the following association between a route and a callback in my application:

$api->slim->post('/:accountId/Phone-Numbers/', function($accountId) use ($api) {
    $api->createPhoneNumber($accountId);
});

What I want to avoid is having the route myhost/a7b81cf/phone-numbers/ return a 404 response because Slim understands the route myhost/a7b81cf/Phone-Numbers/ as being different, due to the usage of uppercase letters. How can I avoid setting up two separate routes that trigger the same callback function?

like image 638
Andrei Oniga Avatar asked Nov 13 '13 12:11

Andrei Oniga


People also ask

Are the routes case sensitive?

Hi there, The routes in Angular are case sensitive.

What is the use of slim framework?

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and returns an HTTP response. That's it.


1 Answers

This is an old question, but I wanted to provide a clear answer for this problem.

There is a 'routes.case_sensitive' configuration that allows you to do that. I don't know exactly why this is not in the docs (http://docs.slimframework.com/#Application-Settings), but if you look at the framework's source code (specifically, in getDefaultSettings() at Slim.php) you can see that is there.

I just tested it and it works fine.

Summarizing, the solution is to apply the 'routes.case_sensitive' configuration like this:

$configurations = [
    // ... other settings ...
    'routes.case_sensitive' => false
];

$app->config($configurations);
like image 83
federicojasson Avatar answered Oct 18 '22 08:10

federicojasson