문제

웹 응용 프로그램을 만들기 위해 Zend-Framework 1.9.5를 사용하고 있지만 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 코드를 하드 코드로 편집하지 않으면 서 수행 할 수 없었습니다.

감사

도움이 되었습니까?

해결책 2

나는이 솔루션을 생각해 냈다. 기능적이기까지 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 도우미 클래스이며 최상의 솔루션은 아니지만 지금은 나와 잘 작동합니다.

다른 팁

링크 A 조회를 생성 할 때는 도우미에게 간단한 부울으로 모든 아파 램터를 제거하도록 요청할 수 있습니다.

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

마지막 매개 변수는 매개 변수를 재설정할지 여부를 알려줍니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top