I am beginner into the symfony framework and I want to get the base url of
symfony3 framework into the view file. 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
So I don't know how to use the base url in the view .php file.
I get the base url into the head.blade.PHP file of Laravel framework using:-
<?php echo URL::to('/'); ?>
I want to like this into symfony3 framework.
As explained on the doc : in symfony you have to create routes.
/**
* @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.
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.
Have you tried this:
$link = $this->generateUrl('routeToPrint',array(),UrlGeneratorInterface::ABSOLUTE_URL);
Where 'routeToPrint' is your annotation route. It returns a URL. You also need:
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
At the top of your file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.