Symfony Twig Extension 破坏了其他服务 - 模板是否在安全之前完成?
Symfony Twig Extension breaks other service - Is templating done before security?
我正在开发 Symfony 2.7 WebApp。我创建的其中一个捆绑包包括一项提供一些与用户相关的东西的服务,例如userHasPurchases()
.
问题是,包含 Twig Extesion
会破坏另一项服务:
AppShopService
namespace AppShopBundle\Service;
use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
...
class AppShopService {
protected $user;
public function __construct(TokenStorageInterface $tokenStorage, ...) {
$this->user = $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null;
...
}
public function userHasPurchases(User $user) {
$user = $user ? $user : $this->user;
$result = $user...
return result;
}
}
AppShopBundle\Resources\config\services.yml
services:
app_shop.service:
class: AppShopBundle\Service\AppShopService
arguments:
- "@security.token_storage"
- ...
到目前为止一切正常:AppShopServices
是用当前用户创建的,userHasPurchases()
按预期工作。
现在我添加了一个 Twig Extension 以便能够在我的模板中使用 userHasPurchases()
:
树枝扩展
namespace AppShopBundle\Twig;
use AppShopBundle\Service\AppShopService;
class AppShopExtension extends \Twig_Extension {
private $shopService;
public function __construct(AppShopService $shopService) {
$this->shopService = $shopService;
}
public function getName() {
return 'app_shop_bundle_extension';
}
public function getFunctions() {
$functions = array();
$functions[] = new \Twig_SimpleFunction('userHasPurchases', array(
$this,
'userHasPurchases'
));
return $functions;
}
public function userHasPurchases($user) {
return $this->shopService->userHasPurchases($user);
}
}
在 AppShopBundle\Resources\config\services.yml
中包含扩展
services:
app_shop.service:
class: AppShopBundle\Service\AppShopService
arguments:
- "@security.token_storage"
- ...
app_shop.twig_extension:
class: AppShopBundle\Twig\AppShopExtension
arguments:
- "@app_shop.service"
tags:
- { name: twig.extension }
包含 Twig Extension
后,AppShopService
及其方法 userHasPurchases
不再有效。问题是,AppShopService
的构造函数不再设置 user
,因为 $tokenStorage->getToken()
现在 returns null
。
这怎么可能?除了 Twig Extension
之外,我没有做任何更改。一旦我从 services.yml
中删除 Twig Extension
,一切都会再次正常工作。
我唯一的猜测是,Twig Extension
的创建是在任何安全性之前完成的。但是为什么?
知道这里可能出了什么问题吗?
不要在构造函数中与 tokenStorage 交互,只能在 userHasPurchases
方法中交互。
namespace AppShopBundle\Service;
use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
...
class AppShopService {
protected $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage, ...) {
$this->tokenStorage = $tokenStorage;
}
public function userHasPurchases(User $user) {
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$result = $user...
return result;
}
}
希望对您有所帮助
我正在开发 Symfony 2.7 WebApp。我创建的其中一个捆绑包包括一项提供一些与用户相关的东西的服务,例如userHasPurchases()
.
问题是,包含 Twig Extesion
会破坏另一项服务:
AppShopService
namespace AppShopBundle\Service;
use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
...
class AppShopService {
protected $user;
public function __construct(TokenStorageInterface $tokenStorage, ...) {
$this->user = $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null;
...
}
public function userHasPurchases(User $user) {
$user = $user ? $user : $this->user;
$result = $user...
return result;
}
}
AppShopBundle\Resources\config\services.yml
services:
app_shop.service:
class: AppShopBundle\Service\AppShopService
arguments:
- "@security.token_storage"
- ...
到目前为止一切正常:AppShopServices
是用当前用户创建的,userHasPurchases()
按预期工作。
现在我添加了一个 Twig Extension 以便能够在我的模板中使用 userHasPurchases()
:
树枝扩展
namespace AppShopBundle\Twig;
use AppShopBundle\Service\AppShopService;
class AppShopExtension extends \Twig_Extension {
private $shopService;
public function __construct(AppShopService $shopService) {
$this->shopService = $shopService;
}
public function getName() {
return 'app_shop_bundle_extension';
}
public function getFunctions() {
$functions = array();
$functions[] = new \Twig_SimpleFunction('userHasPurchases', array(
$this,
'userHasPurchases'
));
return $functions;
}
public function userHasPurchases($user) {
return $this->shopService->userHasPurchases($user);
}
}
在 AppShopBundle\Resources\config\services.yml
中包含扩展services:
app_shop.service:
class: AppShopBundle\Service\AppShopService
arguments:
- "@security.token_storage"
- ...
app_shop.twig_extension:
class: AppShopBundle\Twig\AppShopExtension
arguments:
- "@app_shop.service"
tags:
- { name: twig.extension }
包含 Twig Extension
后,AppShopService
及其方法 userHasPurchases
不再有效。问题是,AppShopService
的构造函数不再设置 user
,因为 $tokenStorage->getToken()
现在 returns null
。
这怎么可能?除了 Twig Extension
之外,我没有做任何更改。一旦我从 services.yml
中删除 Twig Extension
,一切都会再次正常工作。
我唯一的猜测是,Twig Extension
的创建是在任何安全性之前完成的。但是为什么?
知道这里可能出了什么问题吗?
不要在构造函数中与 tokenStorage 交互,只能在 userHasPurchases
方法中交互。
namespace AppShopBundle\Service;
use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
...
class AppShopService {
protected $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage, ...) {
$this->tokenStorage = $tokenStorage;
}
public function userHasPurchases(User $user) {
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$result = $user...
return result;
}
}
希望对您有所帮助