如何让 Zend_Controller_Router 默认重置不需要的参数

发布于 2024-08-12 17:26:35 字数 1317 浏览 12 评论 0原文

我正在使用 Zend-Framework 1.9.5 制作一个 Web 应用程序,但它的 Url_Helper 在参数重置方面对我来说相当棘手!,我知道这是一个很好的功能(参数保留),但在大多数情况下我不这样做不需要它! 所以我正在考虑覆盖默认路由器以强制它丢失参数,除非我要求它或者指定它保留的某些参数(lang,或类似的东西)。

另外,我想将其设为默认路由器,这样我就不必编辑我的控制器、视图来完成此任务!

有什么建议吗?

更新: 我花了整个上午尝试编写我的 url 帮助器 Admin_View_Helper_Xurl,但我无法做任何解决问题的事情:

<?php
class Admin_View_Helper_Xurl extends Zend_View_Helper_Abstract
{
     public function xurl(array $urlOptions = array(), $name = 'default', $reset = false, $encode = true)
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();

        $wanted_params = array('module', 'controller', 'action', 'lang', 'page', 'search');

        $route = $router->getCurrentRoute();

        $something = anyWayToGetThatObjectOrClass();

        $params = $something->getParams();

        foreach($params as $key => $val) {
            if (!in_array($key, $wanted_params)) {
                $params[$key] = null; // OR uset($params[$key]);
            }
        }

        $something->clearParams();
        $something->setParams($params);

        return $router->assemble($urlOptions, $name, $reset, $encode);
    }
}

我尝试获取当前的 URL 参数并过滤它们并清除当前参数并传递我的过滤了,但如果不硬编码编辑一个 Zend_Framework 代码,我就无法做任何事情:(。

谢谢

I'm using Zend-Framework 1.9.5 to make a web-application, But it's Url_Helper was quite tricky to me in the matter of parameter reset!, I know it's a good feature (parameter preserving) but in most cases I don't need it!.
So I'm thinking of overriding the default Router to force it loosing parameters Unless I ask for it or maybe specifying a certain parameters that it keeps like (lang, or something like that).

Also I want to make it the default router so I don't have to edit my Controllers, Views to get that done!

Any suggestions?

Update:
I spent the whole morning trying to write my url helper Admin_View_Helper_Xurl, But I couldn't do anything that solves the problem:

<?php
class Admin_View_Helper_Xurl extends Zend_View_Helper_Abstract
{
     public function xurl(array $urlOptions = array(), $name = 'default', $reset = false, $encode = true)
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();

        $wanted_params = array('module', 'controller', 'action', 'lang', 'page', 'search');

        $route = $router->getCurrentRoute();

        $something = anyWayToGetThatObjectOrClass();

        $params = $something->getParams();

        foreach($params as $key => $val) {
            if (!in_array($key, $wanted_params)) {
                $params[$key] = null; // OR uset($params[$key]);
            }
        }

        $something->clearParams();
        $something->setParams($params);

        return $router->assemble($urlOptions, $name, $reset, $encode);
    }
}

I tried to get current URL parameters and filter them and clear the current parameters and pass my filtered ones but I couldn't do anything that does it without hard-code editing one Zend_Framework code :(.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

凉薄对峙 2024-08-19 17:26:35

当生成链接视图时,您可以要求助手使用简单的布尔值删除所有参数:

<?php echo $this->url(array('controller' => 'index', action => 'action'), 'default', true); ?>

最后一个参数告诉是否重置参数。

When generating a link a view, you can ask the helper to get rid of all aparamters with a simple boolean :

<?php echo $this->url(array('controller' => 'index', action => 'action'), 'default', true); ?>

The last parameter tells whether to reset parameters or not.

咋地 2024-08-19 17:26:35

我想出了这个解决方案。花了7个小时才发挥作用。

class Zend_View_Helper_Xurl extends Zend_View_Helper_Abstract
{

    const RESET_ALL = 'all';
    const RESET_CUSTOM = 'normal';
    const RESET_NON_MVC = 'mvc';
    const RESET_NONE = 'none';


    protected $_wantedParams = array('module', 'controller', 'action', 'lang', 'page', 'search');
    protected $_router;  
    /**
     * Generates an url given the name of a route.
     *
     * @access public
     *
     * @param  array $urlOptions Options passed to the assemble method of the Route object.
     * @param  mixed $name The name of a Route to use. If null it will use the current Route
     * @param  bool $reset Whether or not to reset the route defaults with those provided
     * @return string Url for the link href attribute.
     */

    public function __construct()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $this->_router = clone $router;
    }

    public function xurl(array $urlOptions = array(), $reset = 'mvc', $encode = true)
    {
        $urlOptions = $this->_getFilteredParams($urlOptions, $reset);
        return $this->_router->assemble($urlOptions, $name, true, $encode);
    }

    protected function _getFilteredParams($data = array(), $level)
    {
        // $filteredValues = array();
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $filteredValues = $request->getUserParams();
        $filteredValues['module']     = $request->getModuleName();
        $filteredValues['controller'] = $request->getControllerName();
        $filteredValues['action']     = $request->getActionName();


        switch ($level) {
            case self::RESET_ALL:
                $filteredValues['module'] = null;
                $filteredValues['controller'] = null;
                $filteredValues['action'] = null;
            // break omitted intentionally
            case self::RESET_NON_MVC:
                $filteredValues['page'] = null;
                $filteredValues['lang'] = null;
                $filteredValues['search'] = null;
            // break omitted intentionally

            case self::RESET_CUSTOM:
                foreach ($filteredValues as $key=>$val) {
                    if (!in_array($key, $this->_wantedParams)) {
                        $filteredValues[$key] = null;
                    }
                }
                break;
            case self::RESET_NONE:
                break;

            default:
                throw new RuntimeException('Unsuppoted Xurl URL helper reset level.');
                break;
        }


        foreach ($filteredValues as $key => $val) {
            if (!array_key_exists($key, $data)) {
                $data[$key] = $val;
            }
        }

        return $data;
    }
}

显然,它是一个 View Helper 类,可能不是最好的解决方案,但目前对我来说效果很好。

I came up with this solution. It took 7 hours to be functional.

class Zend_View_Helper_Xurl extends Zend_View_Helper_Abstract
{

    const RESET_ALL = 'all';
    const RESET_CUSTOM = 'normal';
    const RESET_NON_MVC = 'mvc';
    const RESET_NONE = 'none';


    protected $_wantedParams = array('module', 'controller', 'action', 'lang', 'page', 'search');
    protected $_router;  
    /**
     * Generates an url given the name of a route.
     *
     * @access public
     *
     * @param  array $urlOptions Options passed to the assemble method of the Route object.
     * @param  mixed $name The name of a Route to use. If null it will use the current Route
     * @param  bool $reset Whether or not to reset the route defaults with those provided
     * @return string Url for the link href attribute.
     */

    public function __construct()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $this->_router = clone $router;
    }

    public function xurl(array $urlOptions = array(), $reset = 'mvc', $encode = true)
    {
        $urlOptions = $this->_getFilteredParams($urlOptions, $reset);
        return $this->_router->assemble($urlOptions, $name, true, $encode);
    }

    protected function _getFilteredParams($data = array(), $level)
    {
        // $filteredValues = array();
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $filteredValues = $request->getUserParams();
        $filteredValues['module']     = $request->getModuleName();
        $filteredValues['controller'] = $request->getControllerName();
        $filteredValues['action']     = $request->getActionName();


        switch ($level) {
            case self::RESET_ALL:
                $filteredValues['module'] = null;
                $filteredValues['controller'] = null;
                $filteredValues['action'] = null;
            // break omitted intentionally
            case self::RESET_NON_MVC:
                $filteredValues['page'] = null;
                $filteredValues['lang'] = null;
                $filteredValues['search'] = null;
            // break omitted intentionally

            case self::RESET_CUSTOM:
                foreach ($filteredValues as $key=>$val) {
                    if (!in_array($key, $this->_wantedParams)) {
                        $filteredValues[$key] = null;
                    }
                }
                break;
            case self::RESET_NONE:
                break;

            default:
                throw new RuntimeException('Unsuppoted Xurl URL helper reset level.');
                break;
        }


        foreach ($filteredValues as $key => $val) {
            if (!array_key_exists($key, $data)) {
                $data[$key] = $val;
            }
        }

        return $data;
    }
}

Clearly it's a View Helper class, may be not the best solution but it works fine with me for now.

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