【问题标题】:Is it possible to get current url with zend url helper reseting parameters and not passing urlOptions?是否可以使用 zend url helper 重置参数而不传递 url 选项来获取当前 url?
【发布时间】:2011-10-05 05:05:22
【问题描述】:

如果我从视图中调用 $this->url(),我会得到带有参数的 url。例如:/test/view/var1/value1

有没有什么方法可以在不带参数(var1/value1)且不传递 urlOptions 的情况下获取当前 url/位置:

例如,如果我使用它,它会起作用:

$this->url(array("controller"=>"test", "action"=>"view"),null,true); //返回/test/view

但我想避免传递参数

$this->url(array(),null,true);

有什么办法吗?

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    这听起来像是视图助手的工作。像这样?

    class Zend_View_Helper_Shorturl {
    
        public function shorturl() {
            $request = Zend_Controller_Front::getInstance()->getRequest();
            $module = $request->getModuleName();
            $controller = $request->getControllerName();
            $action = $request->getActionName();
            return $this->view->url(array('module'=>$module, 'controller'=>$controller,'action'=>$action), null, true);
            //return "/$controller/$action"; //Left this in incase it works better for you.
    
        }
    }  
    

    那你就在你的视图里写$this->shorturl();

    为了清楚起见,这将进入scripts/helpers/Shorturl.php

    编辑: 事实上,我刚刚尝试过这个并且它有效。我会说这是使用的解决方案。

    class Zend_View_Helper_Shorturl {
    
        public function shorturl() {
            return $this->view->url(array('module'=>$module, 'controller'=>$controller,'action'=>$action), null, true);
        }
    }
    

    【讨论】:

    • 我会说这几乎是比我上面的解决方案更好的解决方案,因为它仅在使用时才被调用。但是,最后一行应该是 return $this->view->url(array('controller'=>$controller,'action'=>$action), null, true);,因为默认路由器可能不会使用 /controller/action 形式的 URL。
    • 好点,编辑以反映您的评论并考虑模块。
    【解决方案2】:

    并非如此 - 路由器(由 url 视图助手调用)用来生成链接的 Zend_Controller_Request_Http 对象并不能真正区分模块/控制器/操作参数和您的操作可能使用的其他参数。

    要么使用您上面引用的第一种形式,要么如果您需要一个适用于每个动作/控制器的解决方案,请创建如下内容:

    class MyApplication_Controller_Action_Base extends Zend_Controller_Action {
        public function preDispatch() {
            //Generate a URL to the module/controller/action 
            //(without any other parameters)
            $this->view->bareUrl = $this->view->url(
                array_intersect_key(
                    $this->getRequest()->getParams(),
                    array_flip(array('module','view','controller')
                ),
                null,
                true
            );
        }
    }
    

    在任何视图中,您都可以使用<?=$this->bareUrl?>

    【讨论】:

      【解决方案3】:

      只需使用 $this->url() 来获取 baseUrl/controller,就像它出现在浏览器 URL 中一样,例如,如果它是 www.your-domain.ro/project/members 它将返回项目/成员

      【讨论】:

        猜你喜欢
        • 2021-09-29
        • 2010-12-18
        • 2011-05-26
        • 1970-01-01
        • 2012-06-19
        • 2017-01-12
        • 1970-01-01
        • 2012-11-03
        • 2012-04-19
        相关资源
        最近更新 更多