2017-11-23 9 views
0

에로 끝나는 모든 경로를 리디렉션, fony 프로젝트는 우리가 다음과 같은 경로를 가정하자 :심포니 내 Sym을지나 URL

$route= new Route('/my-method',array(
    '_controller'=>'AppBundle:MyController:myMethod' 
)); 
$collection->add('my_method_route',$route); 

$route= new Route('/my-method2',array(
    '_controller'=>'AppBundle:MyController:myAnotherMethod' 
)); 
$collection->add('my_method2_route',$route); 

$route= new Route('/anotherStuff',array(
    '_controller'=>'AppBundle:MyController:anotherStuff' 
)); 
$collection->add('another_stuff',$route); 

$route= new Route('/anotherStuff/{param}',array(
    '_controller'=>'AppBundle:MyController:anotherStuffOneParam' 
)); 
$collection->add('another_stuff',$route); 

$route= new Route('/anotherStuff/{param}/{another_param}',array(
    '_controller'=>'AppBundle:MyController:anotherStuffTwoParam' 
)); 
$collection->add('another_stuff',$route); 

그리고 URL이 / 예로 끝나는 때 나는 원한다. http//example.com/my-method/http//example.com/my-method으로 리디렉션하십시오.

경로에서 첫 번째 매개 변수로 regex를 사용하거나 리디렉션을 수행하기 위해 Symfony의 라우팅 시스템 showhow를 구성 할 수 있다면 생각했습니다.

하지만 Symfony는 정규식을 사용하거나 URL에 선택적으로 삽입 할 수 있습니까? 예를 들어 '/my-method2/?'을 사용하여 경로가 /으로 끝나고 theese 항목을 무시한다는 것을 알리십시오.

+1

당신은 심지어 일부 구성 웹 서버 수준에서이 작업을 수행 할 수 있습니다 전혀 심포니 필요하지 않습니다는. – DonCallisto

답변

0

후행 슬래시로 끝나는 모든 URL을 동일한 URL로 리디렉션하고 싶습니까?

일치하는 경로를 만들어야합니다. */$ 컨트롤러에서이 "/"를 제거하십시오.

그리고 심포니 문서에 코드를주는 기사가있다 : https://symfony.com/doc/current/routing/redirect_trailing_slash.html

+0

어떤 이유로 리디렉션 URL에 슬래시 2 개를 추가합니다. –

+0

당신은 당신이 모든 노선 정의의 끝 부분에 경로를 넣었습니까? 다음 코드는 $ url = str_replace ($ pathInfo, rtrim ($ pathInfo, '/'), $ requestUri); 후행 슬래시를 제거하고 URL에서이 부분을 바꿉니다. 끝에 "/"가 두 개있는 것은 이상한 것 같습니다. – Bacteries