Zend Framework

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

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Trivial Trivial
  • Resolution: Fixed
  • Affects Version/s: 1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.5.0RC1, Next Major Release
  • Fix Version/s: 1.5.0
  • Component/s: Zend_Controller
  • Labels:
    None
  • Fix Version Priority:
    Should Have

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:

$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:

/**
     * 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:

/**
     * 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;
    }

Activity

Hide
Matthew Weier O'Phinney added a comment -

Scheduling for 1.5.0RC2

Show
Matthew Weier O'Phinney added a comment - Scheduling for 1.5.0RC2
Hide
Matthew Weier O'Phinney added a comment -

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

Show
Matthew Weier O'Phinney added a comment - Fixed in trunk and release branch, with corresponding unit test.

People

Vote (0)
Watch (0)

Dates

  • Due:
    Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
10m
Original Estimate - 10 minutes
Remaining:
10m
Remaining Estimate - 10 minutes
Logged:
Not Specified
Time Spent - Not Specified