silexphp / Silex

[DEPRECATED -- Use Symfony instead] The PHP micro-framework based on the Symfony Components
https://silex.symfony.com
MIT License
3.58k stars 718 forks source link

Method "Silex\Route::secure" does not exist. #1084

Closed tineo closed 9 years ago

tineo commented 9 years ago

Controller.php line 89

PHP 5.5 Silex 1.2.2

i'm using use Silex\Route\SecurityTrait;

for

$app->get('/', function () use ($app) { return $app['twig']->render('index.html', array()); }) ->secure('ROLE_USER');

and get this error

Method "Silex\Route::secure" does not exist.

srsbiz commented 9 years ago

Have you defined your own Route class as described in http://silex.sensiolabs.org/doc/usage.html#traits ?

fabpot commented 9 years ago

Closing as there is no feedback from reporter.

DracotMolver commented 8 years ago

Sorry guys but I'm facing the same issue but i will send you more info. I'm not using my own routes but, I'm using bind functions, but I used bind functions with assets and everything works fine. So the issue is this

'BadMethodCallException' with message 'Method "Silex\Route::secure" does not exist.' in  vendor/silex/silex/src/Silex/Controller.php:90
Stack trace:
#0 Silex\Controller->__call('secure', Array)
#1 Silex\Controller->secure('ROLE_ADMIN')
#2 {main}
  thrown in /vendor/silex/silex/src/Silex/Controller.php on line 90

My code

$app->get('/auth/documentos', function(Request $request) use ($app)
     $token = $app['security.token_storage']->getToken();
     if ($token !== null) {
          if ($token->getUsername() === 'qwerty' && $token->isAuthenticated()) {
               return $app['twig']->render('documentos.html');
          } else {
               return $app->abort(403);
          }
      } else {
          return $app->redirect($app['url_generator']->generate('page_login'));
      }
})->bind('page_documentos')
  ->secure('ROLE_ADMIN')
GromNaN commented 8 years ago

You have to use a custom Route class including the SecurityTrait in order to have this method defined.

DracotMolver commented 8 years ago

right man, I haven't read the doc. My mistake, thanks :)