我想在本地浏览器cookie中存储一些信息.经过几个小时寻找一个很好的教程,我设法将一些数据存储在非会话cookie中:
controller - indexAction()
$cookieGuest = array(
'name' => 'mycookie',
'value' => 'testval',
'path' => $this->generateUrl('my_route'),
'time' => time() + 3600 * 24 * 7
);
$cookie = new Cookie($cookieGuest['name'], $cookieGuest['value'], $cookieGuest['time'], $cookieGuest['path']);
$response = new Response();
$response->headers->setCookie($cookie);
$response->send();
Run Code Online (Sandbox Code Playgroud)
我想知道这是不是正确的方法.此外,我尝试了几种方法来使用HttpFoundation组件读取cookie,但没有成功.除了通过$ _COOKIE ['mycookie']访问cookie之外还有其他方式吗?
这是我尝试阅读cookie的地方
controller - cookieAction()
public function cookieAction($_locale, $branch, $page)
{
$response = new Response();
$cookies = $response->headers->getCookies();
var_dump($cookies);
// TODO: Get params for indexAction from cookie if available
return $this->indexAction($_locale, $branch, $page);
}
Run Code Online (Sandbox Code Playgroud) 基于这个问题,我想基于协商的子协议创建一个服务器端点实例,以不同的方式处理各种协议消息.不幸的是ServerEndpointConfig.Configurator.getEndpointInstance[ docs ]不允许我访问任何相关的会话数据来获得协商的子协议,所以我可以实例化不同的类.
public static class ServerEndpointConfigurator extends
ServerEndpointConfig.Configurator {
public ServerEndpointConfigurator()
{
}
@Override
public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
// useful to work with session data in endpoint instance but not at getEndpointInstance
HttpSession httpSession = (HttpSession) request.getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(), httpSession);
}
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
// TODO get negotiated subprotocol and instantiate endpoint using switch case or factory
return (T) new WebSocketControllerA();
// or return (T) …Run Code Online (Sandbox Code Playgroud) 为了给我创建一个导航webinterface,我想从我的bundle的路由配置中获取一个变量.我在mybundle/Resources/config/routing.yml中定义了可用的页面.
mybundle_homepage:
pattern: /{_locale}/{branch}/{page}
defaults: { _controller: mybundle:mycontroller:index, _locale: de, branch: x.x.x, page: start }
requirements:
_locale: de|en
page: start|functions|events|constants|styleguide
Run Code Online (Sandbox Code Playgroud)
现在我看一下Symfony2 YAML Parser,我必须为它的静态方法解析提供一个文件路径:http://symfony.com/doc/2.0/reference/YAML.html
mycontroller.php
use Symfony\Component\Yaml\Yaml;
class mycontroller extends Controller
{
public function indexAction($_locale, $branch, $page)
{
$routing = Yaml::parse('../Resources/config/routing.yml');
var_dump($routing);
}
}
Run Code Online (Sandbox Code Playgroud)
我以为我可以这样做,因为文件夹hirarchy看起来像那样:
- mybundle
- 调节器
- mycontroller.php
- Rescources
- 配置
- 使用routing.yml
但它不起作用.从路由文件中获取requirements.page数组的任何想法或其他方法?
问候,本
我在从字符串创建向量时遇到问题.有人可以解释下面4例之间的区别吗?根据cplusplus.com我的预期Case 1工作方式与Case 2出于同样奇怪的原因一样,但事实并非如此.
typedef std::vector<uint8_t> uint8vec_t;
std::string keySecret ()
{
return std::string ("SomeSecret");
}
Run Code Online (Sandbox Code Playgroud)
情况1
// throws std::bad_alloc
uint8vec_t vSecret (keySecret ().begin (), keySecret ().end ());
Run Code Online (Sandbox Code Playgroud)
案例2
// results in a vector with strange length
uint8vec_t vSecret (keySecret ().begin (), keySecret ().begin () + keySecret ().length ());
Run Code Online (Sandbox Code Playgroud)
案例3
// throws std::bad_alloc
uint8vec_t vSecret (&keySecret ()[0], &keySecret ()[keySecret ().length ()]);
Run Code Online (Sandbox Code Playgroud)
案例4
// throws std::bad_alloc
uint8vec_t vSecret (&keySecret ()[0], &keySecret ()[0] + keySecret ().length ());
Run Code Online (Sandbox Code Playgroud)