我在我的应用程序路由文件中定义了一个路由:
RouteName:
pattern: /some/route
defaults: { _controller: MyAppBundle:Controller:action }
Run Code Online (Sandbox Code Playgroud)
在控制器中我可以使用:
$this->get('router')->generate('RouteName');
Run Code Online (Sandbox Code Playgroud)
我如何从我创建的新类中访问它,例如不扩展任何内容的视图类:
namespace My\AppBundle\View;
class ViewClass {
public function uri()
{
return getTheRoute('RouteName');
}
}
Run Code Online (Sandbox Code Playgroud)
Cyp*_*ian 36
您需要将"路由器"服务注入ViewClass.例如.在您定义ViewClass服务的地方:
viewclass.service:
class: Namespace\For\ViewClass
arguments:
router: "@router"
Run Code Online (Sandbox Code Playgroud)
然后在你的构造函数中:
public function __construct(\Symfony\Bundle\FrameworkBundle\Routing\Router $router)
{
$this->router = $router;
}
Run Code Online (Sandbox Code Playgroud)