Possiedo un servizio che ottiene l'utente attualmente connesso, che funziona solo per un po 'di tempo mentre si trova nell'ambiente di sviluppo.TokenStorage a volte restituisce null in Service
Il problema sembra essere ogni volta che cambia il template Twig e rinfresco ottengo l'errore:
Error: Call to a member function getUser() on null
Se mi aggiorna la pagina tutto funziona come dovrebbe finché posso aggiornare di nuovo il template Twig. Questo ovviamente rende lo sviluppo molto lento mentre aggiorno costantemente la pagina.
cose che ho fatto finora: -
- eliminato la cache ambiente dev.
- Cancellata la cache del browser.
- confermato l'utente è sicuramente accesso (altrimenti non avrebbe lavorare il sul secondo aggiornamento)
Qualcuno ha qualche idea che cosa potrebbe essere la causa del problema?
services.yml
myservice:
class: AppBundle\Services\MyService
arguments: ["@doctrine.orm.entity_manager", "@security.token_storage"]
MyService.php
<?php
namespace AppBundle\Services;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class MyService
{
private $em;
private $token;
public function __construct($entityManager, TokenStorageInterface $tokenStorage)
{
$this->em = $entityManager;
$this->token = $tokenStorage->getToken();
}
public function doSomething()
{
$user_id = $this->token->getUser()->getID();
return;
}
}
Ramoscello Template
{{ myservice.doSomething }}
Nota:Questo è il codice scarno che causa ancora il problema
controllo: http://stackoverflow.com/questions/24916638/symfony-dependency-injection-in-twig-extension –
Ciao, hai trovare ciò che la causa del problema? Lo sto vivendo anche io. Grazie! – iamjc015