/** * Generates a URL from the given parameters. * * @param mixed $params The parameter values * @param array $context The context * @param Boolean $absolute Whether to generate an absolute URL * * @return string The generated URL */ public function generate($params, $context = array(), $absolute = false) { $slug = null; $defaults = $this->getDefaults(); if (isset($params['sf_subject']) && !isset($params['engine-slug'])) { // Don't override the current page if it is an engine, or a previously // pushed engine page $slug = aRouteTools::getContextEngineSlug($this); if ($slug) { $params['engine-slug'] = $slug; } else { if (method_exists($params['sf_subject'], 'getEngineSlug')) { $params['engine-slug'] = $params['sf_subject']->getEngineSlug(); } } } if (isset($params['engine-slug'])) { $slug = $params['engine-slug']; aRouteTools::pushTargetEngineSlug($slug, $defaults['module']); unset($params['engine-slug']); } $result = aRouteTools::addPageToUrl($this, parent::generate($params, $context, false), $absolute); if ($slug) { aRouteTools::popTargetEngine($defaults['module']); } return $result; }
/** * Prepends the current CMS page to the URL. * * @param string $url The URL so far obtained from parent::generate * @param Boolean $absolute Whether to generate an absolute URL * * @return string The generated URL */ public static function addPageToUrl(sfRoute $route, $url, $absolute) { $slug = aRouteTools::getContextEngineSlug($route); if (!$slug) { $defaults = $route->getDefaults(); $page = aPageTable::getFirstEnginePage($defaults['module']); if (!$page) { $slug = null; } else { $slug = $page->slug; } } if (!$slug) { throw new sfException('Attempt to generate aRoute URL for module ' . $defaults['module'] . ' with no matching engine page on the site'); } // A route URL of / for an engine route maps to the page itself, without a trailing / if ($url === '/') { $url = ''; } // Ditto for / followed by a query string (missed this before) if (substr($url, 0, 2) === '/?') { $url = substr($url, 1); } $pageUrl = aTools::urlForPage($slug, $absolute); $rr = preg_quote(sfContext::getInstance()->getRequest()->getRelativeUrlRoot(), '/'); // Strip controller off so it doesn't duplicate the controller in the // URL we just generated. Also strip off sf_relative_root if any. // We could use the slug directly, but that would // break if the CMS were not mounted at the root on a particular site. // Take care to function properly in the presence of an absolute URL if (preg_match("/^(?:https?:\\/\\/[^\\/]+)?{$rr}(?:\\/[^\\/]+\\.php)?(.*)\$/", $pageUrl, $matches)) { $pageUrl = $matches[1]; } // Fix double / at beginning when engine is at root of site (think "just a blog") $finalUrl = $pageUrl . $url; $finalUrl = preg_replace('|^//|', '/', $finalUrl); return $finalUrl; }