【问题标题】:symfony 3 get base urlsymfony 3 获取基本 url
【发布时间】:2016-12-28 09:10:59
【问题描述】:

我想像这样在服务 YAML 中获取顶级 URL:

app.meal_list_service:
    class: Acme\MealLogBundle\Service\MealListService
    arguments: ['@app.entity_repository_meal',getBaseUrl()"]

我找不到一个好的解决方案。我尝试从

获取 URL

'%router.request_context.scheme%://%router.request_context.host%/'

但这仅返回第一部分 URL。

【问题讨论】:

  • 示例:%kernel.root_dir%/config/routing.yml

标签: php yaml symfony


【解决方案1】:

编辑:

我的错。我以为你想要根项目目录。 尝试在您的 parameters.yml 中定义项目根路径,并以类似于传递 %kernel.root_dir% 的方式传递它。

// in parameters.yml
my_project_path: https://stackoverflow.com

// in your service.yml
app.meal_list_service:
    class: Acme\MealLogBundle\Service\MealListService
    arguments: 
        - '@app.entity_repository_meal'
        - '%my_project_path%'

【讨论】:

    【解决方案2】:

    你可以这样做:

    app.meal_list_service:
        class: Acme\MealLogBundle\Service\MealListService
        arguments: ['@app.entity_repository_meal', '%request_stack%']
    

    为您服务:

    $baseUrl = $requestStack->getCurrentRequest()->getSchemeAndHttpHost();
    

    【讨论】:

      【解决方案3】:

      您始终可以使用 __ DIR __ 并从中创建路径。有时它比 DI 更快并将参数传递给服务。

      看看这个,我在 trait 中使用它来创建图像上传路径。 https://gist.github.com/poznet/9d9b0a8b707a527fa56d9bb1fa1979ad

      【讨论】:

        猜你喜欢
        • 2015-11-26
        • 1970-01-01
        • 2014-05-28
        • 2011-08-08
        • 1970-01-01
        • 2017-01-25
        • 1970-01-01
        • 2012-12-24
        • 2015-03-07
        相关资源
        最近更新 更多