加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – url_for在后端的前端 – Symfony

发布时间:2020-12-13 13:07:13 所属栏目:PHP教程 来源:网络整理
导读:我用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-ti
我用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读