Pylons/pyramid — Issue #406: route_prefix doesn't allow the prefix pattern to match without a trailing slash
Author: mmerickel
Created: 2012-01-18 16:14:35+00:00
State: open
Labels: enhancements

The issue here is an ambiguity in handling `route_prefix`. In general, things work correctly:

```python
def routes(config):
config.add_route('add_user', '/add')

config.include(routes, route_prefix='/users')

# resulting pattern: '/users/add'
```

The ambiguity arises when the included function attempts to add a route using the pattern `'/'` or `''` (an empty string). Pyramid elects to treat the prefix as a container.

```python
def routes(config):
config.add_route('users', '/')

config.include(routes, route_prefix='/users')

# resulting pattern: '/users/'
```

Remember that in Pyramid's `config.add_route` there is no difference between prepending a `'/'` or leaving no prefix. Thus, the two route definitions below result in the same url being matched and generated.

```python
config.add_route('r1', '/route')
config.add_route('r2', 'route')
```

There are 4 possible use cases to account for when looking at the issue.

**Case 1**

```python
def routes(config):
config.add_route('route', '')

config.include(routes, route_prefix='/prefix')
```

**Case 2**

```python
def routes(config):
config.add_route('route', '/')

config.include(routes, route_prefix='/prefix')
```

**Case 3**

```python
def routes(config):
config.add_route('route', '')

config.include(routes, route_prefix='/prefix/')
```

**Case 4**

```python
def routes(config):
config.add_route('route', '/')

config.include(routes, route_prefix='/prefix/')
```

**Discussion**

Pyramid currently treats the route prefix as a container, thus the resulting route will always be appended with a `'/'`. This may not be ideal, but it at least allows for a workaround where the user can add their own route (at the level of the include):

```python
def routes(config):
config.add_route('add', '/add')

config.include(routes, route_prefix='/users')
config.add_route('users', '/users')
```

If there is a clear way to handle each use-case, and document it as such, then it may be possible to change this behavior but to me it's hard to expect a user to "do the right thing" with case 2 due to the way pyramid currently handles `add_route` patterns with and without a prefixed slash.