ZF-10923: Url action helper adds a slash in front of url

Issue Type: Bug Created: 2011-01-07T02:03:59.000+0000 Last Updated: 2012-05-30T12:14:06.000+0000 Status: Resolved Fix version(s): Reporter: Dmitry Petrov (can3p) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_Controller

  • FixForZF1.12
  • zf-caretaker-adamlundrigan

Related issues: - ZF-10091

Attachments: - 50a1a8.png


Zend_Controller_Action_Helper_Url always adds a slash in front of generated url. So, if baseUrl is defined. it leads to the incorrect urls:

$this->_helper->url('action', 'controller', 'module')

=> '/'


Posted by Matthew Weier O'Phinney (matthew) on 2011-01-07T04:47:00.000+0000

Removing the leading slash would break existing applications, as the URLs generated would now be relevant to the current page, not the document root.

How are you defining the baseUrl? Is it being auto-determined for you, or have you set it explicitly? I'm guessing, based on the URL you display above, that you are setting it explicitly. The baseUrl should only contain path information, not the protocol and host -- if you need the protocol and host, you should use the serverUrl() view helper, or grab the information from the request object.

Posted by Dmitry Petrov (can3p) on 2011-01-07T08:50:03.000+0000

I'm setting baseUrl in application.ini config.

You can look through the class source code. It gets basDir from front controller if availible and adds it to the url. After that it adds the leading slash without any conditions.

The right behavior is if leading slash would be added only if baseUrl was not defined in front controller.

Posted by Markus (rockaut) on 2011-04-26T10:13:30.000+0000

And hasn't changed since then. Infact i remember two more issue dubles.

Posted by Adam Lundrigan (adamlundrigan) on 2012-05-29T16:54:04.000+0000

Confirmed. If a fully-qualified baseUrl is supplied to the front controller, then the bug outlined in this ticket manifests itself. Troublesome code: !50a1a8.png!

Unit test:

<pre class="highlight">
Index: tests/Zend/Controller/Action/Helper/UrlTest.php
--- tests/Zend/Controller/Action/Helper/UrlTest.php (revision 24820)
+++ tests/Zend/Controller/Action/Helper/UrlTest.php (working copy)
@@ -178,6 +178,21 @@
         $url = $this->helper->simple('action', null, null, array('foo' => 'bar'));
         $this->assertEquals('/baseurl/module/controller/action/foo/bar', $url);
+    /**
+     * @group ZF-10923
+     */
+    public function testFullyQualifiedBaseUrlDoesNotGetSlashPrepended()
+    {
+        $this->front->setBaseUrl('<a href=""></a>');
+        $request = $this->front->getRequest();
+        $request->setModuleName('module')
+                ->setControllerName('controller');
+        $url = $this->helper->simple('action', null, null, array('foo' => 'bar'));
+        $this->assertEquals('<a href="">…</a>', $url);
+    }
 // Call Zend_Controller_Action_Helper_UrlTest::main() if this source file is executed directly.

And result of running it:

<pre class="highlight">
There was 1 failure:

1) Zend_Controller_Action_Helper_UrlTest::testFullyQualifiedBaseUrlDoesNotGetSlashPrepended
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-<a href="">…</a>
+/<a href="">…</a>

Tests: 1, Assertions: 1, Failures: 1.

Posted by Adam Lundrigan (adamlundrigan) on 2012-05-30T12:12:20.000+0000

After discussing the issue with [~dasprid], this is the intended behavior of setBaseUrl. That method's primary purpose is to allow Zend Framework applications to run in sub-folders by specifying the path prefix that should be stripped from the request URI before being processed by the router. This request URI is an absolute path, not a fully-qualified URI, and so setting the base URL to a fully-qualified URL will break this feature (and the view helper too).

If you need to use the FQDN URL in your view scripts, you can wrap the call to the URL view helper in a call to the serverUrl helper:

<pre class="highlight">
<?php echo $this->serverUrl($this->url(...)); ?>

Or set the base tag in the header of your HTML page:

<pre class="highlight">

I've updated the relevant manual pages (SVN r24827) with a note about fully-qualified base URLs not being supported.

Have you found an issue?

See the Overview section for more details.


© 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.