The example in this article describes how Symfony2 implements the method of obtaining the url in the controller. Share it with everyone for your reference, the details are as follows:
// 假设当前URL地址是http://192.168.1.100/demo/web/app_dev.php/m/index $request = $this->getRequest(); // http or https 此处为http $request->getScheme(); // 192.168.1.100 $request->getHttpHost(); // http://192.168.1.100 $request->getSchemeAndHttpHost(); // /demo/web/ $request->getBasePath(); // /demo/index.php $request->getBaseUrl(); // eg: /demo/web/app_dev.php/user/login 不一定是/demo/web/app_dev.php/m/index 取决于routeName $this->generateUrl('routeName'); // routeName $routeName = $request->get('_route');
I hope this article will be helpful to everyone’s PHP program design based on the Symfony framework.
For more related articles on Symfony2’s method of obtaining the url in the controller, please pay attention to the PHP Chinese website!