Code snippets for symfony 1.x


Refine Tags

Snippets tagged "route"

Generating Symfony URLs in other projects and subdirectories

Integrating Phorum into a Symfony project at, I wanted to use my existing Symfony URLs in my Phorum templates:

// This path should be the path to your config/ProjectConfiguration.class.php file
// in your Symfony project
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', sfConfig::get("sf_environment"), true);
$instance = sfContext::createInstance($configuration);
$routing = $instance->getRouting();
// Our URLs may have a prefix (eg /forum/index.php).  Remove this
$routingOptions = $routing->getOptions();
$urlPrefix = (array_key_exists("prefix", $routingOptions["context"]) ? $routingOptions["context"]["prefix"] : "");
$myURL = $routing->generate("default", array("module" => "foo", "action" => "bar"));
$myURL = str_replace($urlPrefix, "", $myURL);
by Rich Sage on 2010-05-06, tagged phorum  route  url 

Case-insensitive URLs/routes

This works for symfony 1.2 and might be backwards compatible with 1.1 and 1.0, but I haven't tested it with those versions.

Place this file in an appropriate directory (eg. apps/myapp/lib/routing):


class sfRegexExtendedRoute extends sfRoute
  protected function compile()
    $return = parent::compile();
    // Case-insensitive matching (default symfony behaviour = NOT case-insensitive).
    if (isset($this->options['case_sensitive']) && !$this->options['case_sensitive'])
      // Make sure $this->regex is in the form "#...#[???]"
      // and that [???] does not already include "i" (the case-insensitive flag).
      if (($parts = explode('#', strrev($this->regex)))
          && 3 <= ($num_parts = count($parts))
          && '' == $parts[$num_parts-1]
          && false === strpos($parts[0], 'i'))
        $this->regex .= 'i';
    return $return;

Add the option case_sensitive: false in the route you want to make case-insensitive and set its class to sfRegexExtendedRoute:


  url:      /my/case/insensitive/url/:whatever
  options:  { case_sensitive: false }
  class:    sfRegexExtendedRoute

Clear your cache:

> php symfony cc

Your url matching for that route should now be case-insensitive.

Changing case_sensitive: false to case_sensitive: true in routing.yml will revert the matching for that route back to (symfony default) case-sensitive matching.

by Nickolas Daskalou on 2009-05-24, tagged caseinsensitive  casesensistive  expression  regex  regular  route  routes  routing