【问题标题】:url_for in backend for frontend - Symfonyurl_for 在后端用于前端 - Symfony
【发布时间】:2012-01-23 07:21:02
【问题描述】:

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

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

【问题讨论】:

    标签: php symfony1 routing symfony-1.4


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2022-07-17
      • 2021-05-02
      • 2011-11-07
      • 2023-03-11
      • 2016-11-15
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多