【发布时间】:2021-06-06 08:45:21
【问题描述】:
如何只获取 Controller 类的路由?就像在这种情况下是/book
控制器:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/book")
*/
class BookController extends AbstractController
{
/**
* @Route("/")
*/
public function index() : Response
{
return $this->render('book.html.twig');
}
/**
* @Route("/something")
*/
public function doSomething(){
// do stuff
// get the main path/route of this controller; that is '/book', and not '/book/something'
// do stuff
}
}
我发现了这个:$path = $this->getParameter('kernel.project_dir')。这并不重要,但我希望有类似的东西。
【问题讨论】:
-
那种东西都是编译和缓存的,所以我不认为 /book 本身存储在某个地方。也许您可以解释为什么需要它,并且可以建议一种替代方法。否则,只需将其存储为类常量。
标签: symfony path routes controller