P
P
Peter2013-12-15 17:52:24
PHP
Peter, 2013-12-15 17:52:24

Silex - why don't Traits work?

I want to "get" the username of the user. In the doc they write:

Silex\Application\SecurityTrait adds the following shortcuts:
user: Returns the current user.
$user = $app->user();

However, adding use Silex\Application\SecurityTrait to the controller file didn't have the desired effect.
$user = $app->user() throws an error:
Fatal error: Call to undefined method Silex\Application::user() in

On php 5.4 server.
Had a similar problem with
use Application\UrlGeneratorTrait;
, but, at that time, did not attach importance to the problem.
Why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai Alexandrov, 2013-12-15
@dos

And so it works?

$token = $app['security']->getToken();
if (null !== $token) {
    $user = $token->getUser();
}

D
d1mk0, 2015-04-22
@d1mk0

class Application extends Silex\Application
{
    use Silex\Application\TwigTrait;
}
 
$app = new Application();
 
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__ . '/../views',
));
 
$app->get('/', function () use ($app) {
    return $app->render('home.twig');
});
 
$app->run();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question