简体   繁体   English

Symfony 3 - 不包括特定路径(url)开头的路由

[英]Symfony 3 - Route excluding beginning of specific path (url)

I'm creating an application with Symfony 3.2.9, and I would to do a Admin panel to manage application.我正在用 Symfony 3.2.9 创建一个应用程序,我会做一个管理面板来管理应用程序。 Application works like CMS, so is creating new pages with URL, like domain.com/pagename1 and also domain.com/pagename1/subpagelevel2 ect.应用程序的工作方式类似于 CMS,因此使用 URL 创建新页面,例如 domain.com/pagename1 和 domain.com/pagename1/subpagelevel2 等。 Problem is when I want to create URL for Admin panel, and URL should looks like: domain.com/admin, but also admin panel need some sub pages, like domain.com/admin/manage or domain.com/admin/manage/edit/1 ect.问题是当我想为管理面板创建 URL 时,URL 应该看起来像:domain.com/admin,但管理面板也需要一些子页面,如 domain.com/admin/manage 或 domain.com/admin/manage/编辑/1 等。

I created DefaultController with routing like :我创建了 DefaultController 路由如下:

/**
 * @Route("/", name="homepage")
 */

and AdminController with routing like :和 AdminController 的路由如下:

/**
 * @Route("/admin", name="admin")
 */

Problem is that when I want to dynamically create new sub page of application I need to create routing like:问题是,当我想动态创建应用程序的新子页面时,我需要创建如下路由:

/**
 * @Route("/{page}")
 */

But this overwrite my Admin panel sub pages (eg. domain.com/admin/manage).但这会覆盖我的管理面板子页面(例如 domain.com/admin/manage)。

Is it way, to exclude or overwrite path from default DefaultController by AdminController?是否可以通过 AdminController 从默认 DefaultController 中排除或覆盖路径? I want to have possibility to create all URL-s from DefaultController excepts paths beginning like domain.com/admin ... and so on.我希望有可能从 DefaultController 创建所有 URL-s,除了像 domain.com/admin ... 等开头的路径。

From documention in https://symfony.com/doc/current/routing.html you can use the requirements parameter to specify a more strict matchhttps://symfony.com/doc/current/routing.html 中的文档中,您可以使用requirements参数来指定更严格的匹配

I guess something like this whould work:我想这样的事情可行:

DefaultController:默认控制器:

/**
 * @Route("/", name="homepage")
 */

AdminController:管理员控制器:

/**
 * @Route("/admin", name="admin")
 */

Other Controller:其他控制器:

/**
 * we exclude page=admin from this controller
 * @Route("/{page}", requirements={"page": "^(?!admin).+"}))
 */

Routes are searched in the order they are listed - so put the most generic at the end of the list, and it will find and use /admin before /{page}路由是按照它们列出的顺序搜索的 - 所以把最通用的放在列表的末尾,它会在 /{page} 之前找到并使用 /admin

For example, one of my last routes at the bottom of app/conf/routing.yml is例如,我在 app/conf/routing.yml 底部的最后一条路线是

# http://symfony.com/doc/current/routing/redirect_trailing_slash.html
remove_trailing_slash:
    path: /{url}
    defaults:
        _controller: AppBundle:Default:removeTrailingSlash
    requirements:
        url: .*/$
    methods: [GET]

Cleanest on your use case:最干净的用例:

Why not simply create a separate Bundle for Admin and put a prefix in AdminBundle routes?为什么不简单地为 Admin 创建一个单独的 Bundle 并在 AdminBundle 路由中放置一个前缀?

Depend on routing orders and/or REGEX in routes is not recommended just to avoid a Bundle Creation.不推荐依赖路由命令和/或路由中的 REGEX 来避免创建 Bundle。 This is for what Bundles have been thought.这就是 Bundle 的想法。

app/config/routing.yml应用程序/配置/routing.yml

admin:
  resource: "@AdminBundle/Controller/"
  type:     annotation
  prefix:   /admin

Then, all controllers/routes under AdminBundle will work under /admin prefix.然后,AdminBundle 下的所有控制器/路由都将在 /admin 前缀下工作。

For example, IndexController/DefaultController/WhatEverController with this route inside AdminBundle:例如,在 AdminBundle 中使用此路由的 IndexController/DefaultController/WhatEverController:

 /**
 * @Route("/")
 */
public function indexAction()
{
    //My code
}

Will match "/admin" instead of "/"将匹配“/admin”而不是“/”

And:并且:

 /**
 * @Route("/{page}")
 */
public function indexAction()
{
    //My code
}

Will match "/admin/{page}" instead of "/{page}"将匹配“/admin/{page}”而不是“/{page}”

Since Symfony 5.1, you can define route priority :从 Symfony 5.1 开始,您可以定义路由优先级

/**
 * @Route("/admin", name="admin", priority=10)
 */

/**
 * @Route("/{slug}", name="pages", priority=99)
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM