Zend cookie 问题
我正在开发 Zend 1.11 网络应用程序。我构建了一个语言选择器插件,用于根据用户输入切换语言(it 和 en)。
这就是我所拥有的:
class LS_Controller_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
if($request->getParam('lang'))
{
$lang=$request->getParam('lang');
setcookie('lang', $lang, time() + (3600));
echo "we just set a cookie";
}
else if (isset($_COOKIE['lang']))
{
$lang=$_COOKIE['lang'];
echo $lang;
}
else
{ echo 'We are here.But I can't understand why';
$lang='en';
var_dump($_COOKIE);
}
switch(strtolower($lang))
{
case 'en':
$locale="en_US";
break;
case 'it':
$locale="it_IT";
break;
default:
$locale="en_US";
}
$zl=new Zend_Locale();
$zl->setLocale($locale);
Zend_Registry::set('Zend_Locale',$zl);
$translate=new Zend_Translate(
array(
'adapter' => 'gettext',
'content' => APPLICATION_PATH.'/configs/languages/'.$locale.'.mo',
'locale' => 'en'
)
);
Zend_Registry::set('Zend_Translate',$translate);
}
}
[bootstrap.php]
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRoutes()
{
$frontController=Zend_Controller_Front::getInstance();
$router=$frontController->getRouter();
$router->removeDefaultRoutes();
$router->setGlobalParam('lang','en');
$router->addRoute(
'lang',
new Zend_Controller_Router_Route('/:lang/:controller/:action',
array('lang'=>':lang',
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
$router->addRoute(
'langController',
new Zend_Controller_Router_Route('/:controller/:action',
array(
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
$router->addRoute(
'langIndex',
new Zend_Controller_Router_Route('/:lang',
array('lang'=>':lang',
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
$router->addRoute(
'langNothing',
new Zend_Controller_Router_Route('',
array('lang'=>'en',
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
}
}
要点是:
- 我在浏览器中输入完整的 URL mysite.com/it/index/index 并得到回显“我们刚刚设置了一个 cookie”.. 很好我应该能够使用意大利 cookie 集浏览我的网站,但我不能。每当我点击一个链接(例如/index/contactus..links不指定“lang”参数!)时,我会导航到该页面,但它又是英文的(我得到回显消息:“我们在这里。但是我不明白为什么”)。
难道不应该设置cookie吗?
I'm working on a Zend 1.11 webapp. I built a language-selector plugin for switching language (it & en) depending on the user input.
This is what I have:
class LS_Controller_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
if($request->getParam('lang'))
{
$lang=$request->getParam('lang');
setcookie('lang', $lang, time() + (3600));
echo "we just set a cookie";
}
else if (isset($_COOKIE['lang']))
{
$lang=$_COOKIE['lang'];
echo $lang;
}
else
{ echo 'We are here.But I can't understand why';
$lang='en';
var_dump($_COOKIE);
}
switch(strtolower($lang))
{
case 'en':
$locale="en_US";
break;
case 'it':
$locale="it_IT";
break;
default:
$locale="en_US";
}
$zl=new Zend_Locale();
$zl->setLocale($locale);
Zend_Registry::set('Zend_Locale',$zl);
$translate=new Zend_Translate(
array(
'adapter' => 'gettext',
'content' => APPLICATION_PATH.'/configs/languages/'.$locale.'.mo',
'locale' => 'en'
)
);
Zend_Registry::set('Zend_Translate',$translate);
}
}
[bootstrap.php]
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRoutes()
{
$frontController=Zend_Controller_Front::getInstance();
$router=$frontController->getRouter();
$router->removeDefaultRoutes();
$router->setGlobalParam('lang','en');
$router->addRoute(
'lang',
new Zend_Controller_Router_Route('/:lang/:controller/:action',
array('lang'=>':lang',
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
$router->addRoute(
'langController',
new Zend_Controller_Router_Route('/:controller/:action',
array(
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
$router->addRoute(
'langIndex',
new Zend_Controller_Router_Route('/:lang',
array('lang'=>':lang',
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
$router->addRoute(
'langNothing',
new Zend_Controller_Router_Route('',
array('lang'=>'en',
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
}
}
The point is that:
- I type in my browser the full URL mysite.com/it/index/index and I get echo out "we just set a cookie"..wich is fine I should be able to browse my website with the italian cookie set but I don't. Whenever I click over a link (Ex. /index/contactus..links do not specify the 'lang' parameter!) I navigate to that page but it's in english again (I get echoed out the message:"we are here.but I can't understand why").
Shouldn't the cookie be set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,您没有指定 cookie 的路径。因此它将仅设置为当前页面。
如果您浏览到另一个(子)站点,那么您的 cookie 对此路径无效并且不会被传输。您应该将 lang-cookie 设置为“/”路径。
请参阅 setcookie() 文档:
The problem is, that you don't specify the path for your cookie. So it will set for the current page only.
If you browse to another (sub)site, then your cookie is not valid for this path and doesnt get transmitted. You should set your lang-cookie to the "/" Path.
See setcookie() documentation: