Jules
Jules

Reputation: 13

In Symfony2 - The route placeholder and the related controller argument have different type

I have a route as follows:

blog_post:
    path:  /{post_id}/{post_title}.{_format}
    defaults: { _controller: BlogBundle:Blog:post }
    requirements:
        post_id: \d+
        post_title: "[a-zA-Z0-9-]+"
        _format: 'html'

that is matched by the following URL:

www.website.com/32/my-best-story.html

I do not understand why the routing placeholder {post_id} (which is an integer as required in the route) and the related controller argument $post_id do not have the same type:

class BlogController extends Controller
{
    public function postAction(Request $request, $post_id, $post_title)
    {
        $type = gettype($post_id);
        var_dump($type);
        die();
    }
}

returns

string(6) "string"

I would expect it to return:

string(7) "integer"

Where am I wrong? Thanks.

Upvotes: 1

Views: 65

Answers (1)

scoolnico
scoolnico

Reputation: 3135

I do not understand why the routing placeholder {post_id} (which is an integer as required in the route) and the related controller argument $post_id do not have the same type

Be careful, the requirement \d+ means digit not integer

There is no concept of typing in routing placeholder.

Upvotes: 1

Related Questions