ouvjhg
Last Updated: February 25, 2016
·
5.916K
· j7mbo
Mee

Simple silex firewall rules explained

It's annoying trying to set up your first Silex application with a working set of firewall rules. Here's one set that I have tested and works fully. These firewall rules do the following:

  • Allows anonymous access to the homepage (/)
  • Allows anonymous access to the login page (/login)
  • Requires ROLE_USER access for any other page you create (/dashboard, /somethingelse etc)

Feel free to use this as a base for your own Silex projects. I'll shortly be putting up a working Silex with authentication application on my Github.

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'login_path' => array(
            'pattern' => '^/login$',
            'anonymous' => true
        ),
        'default' => array(
            'pattern' => '^/.*$',
            'anonymous' => true,
            'form' => array(
                'login_path' => '/login',
                'check_path' => '/login_check',
            ),
            'logout' => array(
                'logout_path' => '/logout',
                'invalidate_session' => false
            ),
            'users' => $app->share(function($app) { 
                return new App\User\UserProvider($app['db']); 
            }),
        )
    ),
    'security.access_rules' => array(
        array('^/login$', 'IS_AUTHENTICATED_ANONYMOUSLY'),
        array('^/.+$', 'ROLE_USER')
    )
));
Say Thanks
Respond

2 Responses
Add your response

12275
0 7qqhyxwm8rpemsac7 vbyjrsc cbrjkcdttbyjsggcibxgbgi8nx gapuybzzolmm5k9pshbwtbp

Hi
I had the same problem but it looks like in your solution there is one mistake. The path to the /login_check is public which is not right, isn't it?

over 1 year ago ·
13089
1befeceb5e9d893ca88c51c0db652740
over 1 year ago ·