Issues

ZF-2602: Zend_Controller_Action::_redirect() does not work correctly with empty or '/' base

Issue Type: Improvement Created: 2008-02-08T09:05:15.000+0000 Last Updated: 2008-03-21T16:25:29.000+0000 Status: Resolved Fix version(s): - 1.5.0 (17/Mar/08)

Reporter: Ben Scholzen (dasprid) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Controller

Related issues: Attachments:

Description

There is a problem with Zend_Controller_Action::_redirect(), which proxies to Zend_Controller_Action_Helper_Redirector::gotoUrl():

Sample: Application URI: http://www.example.com Application base path: either empty or / Current URI: http://www.example.com/foo

Controller-Command:

<pre class="highlight">
$this->_redirect('bar');

Expected target URI: http://www.example.com/bar

What currently happens: http://www.example.com/foo/bar

However, if the application path is a subdirectory, everything works fine.

The problem is related to the code in the Zend_Controller_Action_Helper_Redirector::_prependBase() function:

<pre class="highlight">
    /**
     * Determine if the baseUrl should be prepended, and prepend if necessary
     *
     * @param  string $url
     * @return string
     */
    protected function _prependBase($url)
    {
        if ($this->getPrependBase()) {
            $request = $this->getRequest();
            if ($request instanceof Zend_Controller_Request_Http) {
                $base = rtrim($request->getBaseUrl(), '/');
                if (!empty($base) && ('/' != $base)) {
                    $url = $base . '/' . ltrim($url, '/');
                }
            }
        }

        return $url;
    }

The problem can easily be fixed by adding two lines:

<pre class="highlight">
    /**
     * Determine if the baseUrl should be prepended, and prepend if necessary
     *
     * @param  string $url
     * @return string
     */
    protected function _prependBase($url)
    {
        if ($this->getPrependBase()) {
            $request = $this->getRequest();
            if ($request instanceof Zend_Controller_Request_Http) {
                $base = rtrim($request->getBaseUrl(), '/');
                if (!empty($base) && ('/' != $base)) {
                    $url = $base . '/' . ltrim($url, '/');
                } else { 
                    $url = '/' . ltrim($url, '/');
                }
            }
        }

        return $url;
    }

Comments

Posted by Matthew Weier O'Phinney (matthew) on 2008-03-06T09:05:52.000+0000

Scheduling for 1.5.0RC2

Posted by Matthew Weier O'Phinney (matthew) on 2008-03-06T21:41:29.000+0000

Fixed in trunk and release branch, with corresponding unit test.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts