Zend cookie 问题

发布于 2024-12-09 19:30:06 字数 3021 浏览 0 评论 0原文

我正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2024-12-16 19:30:06

问题是,您没有指定 cookie 的路径。因此它将仅设置为当前页面

如果您浏览到另一个(子)站点,那么您的 cookie 对此路径无效并且不会被传输。您应该将 lang-cookie 设置为“/”路径。

请参阅 setcookie() 文档:

服务器上可使用 cookie 的路径。如果设置为“/”,则 cookie 将在整个域中可用。如果设置为“/foo/”,则 cookie 将仅在域的 /foo/ 目录和所有子目录(例如 /foo/bar/)内可用。 默认值是设置 cookie 的当前目录

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:

The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文