Search code examples
phpsilex

Silex: Defining string as type hint for route parameter it shows string not exist


We are using Silex, version 2.0.2 and PHP 7.0.1.

In one of the routes a parameter is passed, as follows:

test:
    path:     /auth/{token}
    defaults: { _controller: 'Dummy\Controller::getToken' }
    methods: [GET]
    requirements:
        token: \w+

But when doing type hinting of the controller method as string

public function getToken (Application $ app, Request $ request, string $token)

The following error appears:

Class string does not exist and could not be loaded in vendor/silex/silex/src/Silex/AppArgumentValueResolver.php: 37

Is it not possible to set the type of an argument that is injected through the route configuration?

Thanks.


Solution

  • This might be a bit annoying given you're using version 2.0.2, but this was resolved in version 2.0.3 by this fix: https://github.com/silexphp/Silex/issues/1389

    From the changelog:

    2.0.3 (2016-08-22)
    - fixed lazy evaluation of 'monolog.use_error_handler'
    - fixed PHP7 type hint on controllers

    If you're able to upgrade just a single patch version, the problem should go away. If not, you'll need to remove the type-hint on the controller. It should default to a string anyway, so you won't lose any actual functionality.