我在前端用symfony routing.yml制作了漂亮的URL。在前端,我可以使用例如:

url_for('@news', $news);

这为我产生:
http://mysite.com/frontend_dev.php/news/nice-title/1

但是如果我在后端使用它,我有:
http://mysite.com/backend_dev.php/news/nice-title/1

可能在后端为前端生成这些链接?

最佳答案

在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

关于php - 前端的url_for-Symfony,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8556638/

10-13 02:03