微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – url_for在后端的前端 – Symfony

我用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

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

在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

原文地址:https://www.jb51.cc/php/136376.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐