[英]Getting base_url into a PHP file of symfony3 framework
I am beginner into the symfony framework and I want to get the base url of 我是symfony框架的初学者,我想获取的基本网址
symfony3 framework into the view file. 将symfony3框架放入视图文件。 I did R&D and get how to get the base url into twig file using this.
我进行了研发,并了解了如何使用此方法将基本网址添加到树枝文件中。
{{ app.request.getSchemeAndHttpHost() }}
But my view 但是我的看法
Not twig file it it is .php file 不是树枝文件,它是.php文件
So I don't know how to use the base url in the view .php file. 所以我不知道如何在视图.php文件中使用基本URL。
I get the base url into the head.blade.PHP file of Laravel framework using:- 我使用以下命令将基本URL放入Laravel框架的head.blade.PHP文件中:
<?php echo URL::to('/'); ?>
I want to like this into symfony3 framework. 我想将其添加到symfony3框架中。
As explained on the doc : in symfony you have to create routes. 如doc所述 :在symfony中,您必须创建路由。
/**
* @Route("/", name="site_home")
*/
public function homeAction()
{
// ...
}
In twig, you can then call the routes like this : 在树枝上,您可以像这样呼叫路线:
{{ path('site_home') }}
You also can get the route in the controller and pass it to your view : 您还可以在控制器中获取路由并将其传递给视图:
$homeUrl = $this->get('router')->generate('site_home');
You can get the base_url in controller
and then pass it to the view
file. 您可以在
controller
获取base_url ,然后将其传递到view
文件。
To get the base URL in controller, 要在控制器中获取基本网址 ,
$base_url=$this->container->get('router')->getContext()->getSchemeAndHttpHost();
And pass $base_url
to view and access it the way you want. 并传递
$base_url
以所需的方式查看和访问它。
Have you tried this: 您是否尝试过:
$link = $this->generateUrl('routeToPrint',array(),UrlGeneratorInterface::ABSOLUTE_URL);
Where 'routeToPrint' is your annotation route. 其中“ routeToPrint”是您的注释路线。 It returns a URL.
它返回一个URL。 You also need:
您还需要:
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
At the top of your file. 在文件的顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.