简体   繁体   English

symfony 3获得基本网址

[英]symfony 3 get base url

I want to get top URL in service YAML like this : 我想在服务YAML中获得顶级URL,如下所示:

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

I cant find a good solution for this. 我找不到一个好的解决方案。 I tried getting the URL from 我试过从中获取URL

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

but this return only first part URL. 但这只返回第一部分URL。

Edit: 编辑:

My mistake. 我的错。 I thought you wanted the root project directory. 我以为你想要根项目目录。 Try defining the project root path in your parameters.yml and pass it in a way similar to passing the %kernel.root_dir%. 尝试在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%'

You can do: 你可以做:

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

And in your service: 在您的服务中:

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

You can always use __ DIR __ and create path from it . 您始终可以使用__ DIR __并从中创建路径。 Somtimes it's faster than DI and passing parameters to service. 有时它比DI快,并将参数传递给服务。

Look at this , i'm using it in trait to create path for image upload . 看看这个,我在特质中使用它来创建图像上传路径。 https://gist.github.com/poznet/9d9b0a8b707a527fa56d9bb1fa1979ad https://gist.github.com/poznet/9d9b0a8b707a527fa56d9bb1fa1979ad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM