Search code examples
phpsymfony-1.4

Symfony 1.4 url_for empty string behavior


I'm working on a Symfony 1.4 project and when I do this:

url_for('');

I'm expecting to get the URL for the index.php controller or at least http://domain/ but I'm getting this:

/sfTCPDF

sfTCPDF is a plugin that I have in this project that in my config/ProjectConfiguration.class.php is used like:

class ProjectConfiguration extends sfProjectConfiguration {
    public function setup() {
        $this->enablePlugins('sfTCPDFPlugin');
    }
}

When I disable the plugin I get the expected result:

$url = url_for('');
> /

Or when I use it like this (Doesn't matter if the plugin is enabled or not):

$url = url_for('/');
> /

I don't understand why the plugin is causing (or even if it's the plugin fault).

Any suggestions? I can search & replace the entire project for url_for('') and put the '/' But I really want to understand why this is happening.

After some research: I found out that when the url_for function is called, the procedure is the next:

  • lib/vendor/symfony/lib/helper/UrlHelper.php url_for()
  • lib/vendor/symfony/lib/helper/UrlHelper.php url_for2()
  • lib/vendor/symfony/lib/helper/UrlHelper.php url_for1()
  • lib/vendor/symfony/lib/controller/sfWebController.class.php gen_url()
  • lib/vendor/symfony/lib/routing/sfRoute.class.php generate()

And in the last one it gets the $this->pattern here is where the pattern is sfTCPDF/:action

which means that some routing is playing. Continuing my research i found out that the plugin has a routing.yml with:

sfTCPDF:
    url:   /sfTCPDF/:action
    param: { module: sfTCPDF, action: test }

If I delete this routing everything works as expected.

Now the question is: when do the plugin creates a routing object with the pattern of sfTCPDF and why?


Solution

  • url_for uses the route name you provide to find the proper route in the cached routes table. If you provide an empty string it will take the 0 index from the table (which you can find in the file: cache/app/env/config/config_routing.yml.php.

    When the TCPDF plugin is on its' route is being added as the first one that's why you get it as the url.

    The solution - don't use an empty string for url_for. If you want to get the url of the homepage always use url_for('/') or url_for('@homepage').