我正在寻找一个问题。我开始使用 ZendFramework 3。现在我必须从助手中的 cookie 加载一些数据。
说明:cookie(索引数组)中有历史股票。我正在构建一个小菜单助手,它获取一个列表并渲染它(从 ID 等获取以进行渲染)。我的菜单在所有页面上。我不想进入所有 Controller 来更改操作代码来执行类似的操作
$this->getRequest()->getCookie();
...
return new ViewModel(["history" => $history]);
有没有一种简单的方法可以让助手自动获取历史记录中的值?
谢谢。
最佳答案
我找到了解决办法, 我为助手创建一个工厂,然后将 ContainerInterface 发送给助手:
public function __invoke(ContainerInterface $container, $requestedName, array $options = null){
$em = $container->get('doctrine.entitymanager.orm');
return new MenuWithCookie($container, $em);
}
我在类里面处理它:
class MenuWithCookie extends AbstractHelper
{
protected $_em;
protected $_sm;
public function __construct($container, $em)
{
$this->_sm = $container;
$this->_em = $em;
}
public function __invoke($list, $option = [])
{
if (isset($option['from-cookie']) && $option['from-cookie']) {
$req = $this->_sm->get('Request');
$c = $req->getCookie();
$cookieStuff = (isset($c['cookie-stuff'])) ? $c['history'] : NULL;
}
//Eat them all and do cool stuff
}
}
关于php - ZF3 - 从助手获取 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51282690/