I began to study symphonies and encountered the following problem: There is a method with routing " /show ", and there is a method with " /show/{id} ". Can I somehow combine them?

And this strange behavior: " /show " and " /show/ " for some reason, 2 different routing. Those. if just show specified, the transition to show/ gives 404 error. How to fix it?

Before this Codeigniter. There with routing everything is easier.

  • On the last slash account, you can set up your web server to redirect to a page with a slash / no slash. - apelsinka223

1 answer 1

You can combine two paths by specifying _defaults: { id: 1 } in the directive _defaults: { id: 1 }

 # app/config/routing.yml blog: path: /blog/{page} defaults: { _controller: AppBundle:Blog:index, page: 1 } 

a source

Pages with /show and /show/ paths are different paths, so it’s best to set up a redirect to links with a slash at the end, or vice versa, how you decide.

In principle, you can use the method that is in Symfony CookBook

1. Create a controller:

 // src/AppBundle/Controller/RedirectingController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; class RedirectingController extends Controller { public function removeTrailingSlashAction(Request $request) { $pathInfo = $request->getPathInfo(); $requestUri = $request->getRequestUri(); $url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri); return $this->redirect($url, 301); } } 

Add to the very end of the list of routes a new route:

  remove_trailing_slash: path: /{url} defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash } requirements: url: .*/$ methods: [GET] 

a source