symfony路由文件详解:
①查看所有路由
php bin/console debug:router
②查看指定路由信息
php bin/console debug:router admin_course_list
③根据指定路由名称获取url
如果你的控制器扩展自AbstractController,使用generateUrl()帮助器:
$this->generateUrl('sign_up');
如果你的控制器没有从AbstractController扩展,你需要在你的控制器中获取服务,并遵循下一节的指示:
// src/Service/SomeService.php
namespace App\Service;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class SomeService
{
public function __construct(
private UrlGeneratorInterface $router,
) {
}
public function someMethod()
{
// ...
// generate a URL with no route arguments
$signUpPage = $this->router->generate('sign_up');
// generate a URL with route arguments
$userProfilePage = $this->router->generate('user_profile', [
'username' => $user->getUserIdentifier(),
]);
// generated URLs are "absolute paths" by default. Pass a third optional
// argument to generate different URLs (e.g. an "absolute URL")
$signUpPage = $this->router->generate('sign_up', [], UrlGeneratorInterface::ABSOLUTE_URL);
// when a route is localized, Symfony uses by default the current request locale
// pass a different '_locale' value if you want to set the locale explicitly
$signUpPageInDutch = $this->router->generate('sign_up', ['_locale' => 'nl']);
}
}
