url_for在后端的前端 - Symfony

时间:2011-12-19 02:49:34

标签: php symfony1 routing symfony-1.4

我使用symfony routing.yml在frontend中做了很好的网址。在前端,我可以使用例如:

url_for('@news', $news);

这为我生成:

http://mysite.com/frontend_dev.php/news/nice-title/1

但如果我在后端使用它,我有:

http://mysite.com/backend_dev.php/news/nice-title/1

是否可以在后端为前端生成这些链接?

1 个答案:

答案 0 :(得分:10)

在apps / frontend / config / frontendConfiguration.class.php中使用以下内容:

class frontendConfiguration extends sfApplicationConfiguration
{
  protected $backendRouting = null;

  public function generateBackendUrl($name, $parameters = array(), $absolute = false)
  {
    return sfConfig::get('app_site_url').$this->getBackendRouting()->generate($name, $parameters, $absolute);
  }

  public function getBackendRouting()
  {
    if (!$this->backendRouting )
    {
      $this->backendRouting = new sfPatternRouting(new sfEventDispatcher());

      $config = new sfRoutingConfigHandler();
      $routes = $config->evaluate(array(sfConfig::get('sf_apps_dir').'/backend/config/routing.yml'));

      $this->backendRouting->setRoutes($routes);
    }

    return $this->backendRouting;
  }
}

像这样使用它:

$sf_context->getConfiguration()->generateBackendUrl('my_custom_route', array('id' => 1), true)

此处提供更多信息:http://symfony.com/blog/cross-application-links