简体   繁体   中英

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. Application works like CMS, so is creating new pages with URL, like domain.com/pagename1 and also domain.com/pagename1/subpagelevel2 ect. 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.

I created DefaultController with routing like :

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

and AdminController with routing like :

/**
 * @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).

Is it way, to exclude or overwrite path from default DefaultController by AdminController? I want to have possibility to create all URL-s from DefaultController excepts paths beginning like domain.com/admin ... and so on.

From documention in https://symfony.com/doc/current/routing.html you can use the requirements parameter to specify a more strict match

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}

For example, one of my last routes at the bottom of app/conf/routing.yml is

# 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?

Depend on routing orders and/or REGEX in routes is not recommended just to avoid a Bundle Creation. This is for what Bundles have been thought.

app/config/routing.yml

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

Then, all controllers/routes under AdminBundle will work under /admin prefix.

For example, IndexController/DefaultController/WhatEverController with this route inside AdminBundle:

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

Will match "/admin" instead of "/"

And:

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

Will match "/admin/{page}" instead of "/{page}"

Since Symfony 5.1, you can define route priority :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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