아래 URL 형식으로 두 가지 다른 동작을 호출 할 수있는 경로를 만들려고합니다. something
선택적 스트링이어야 동안 젠드 프레임 워크 2 세그먼트 경로 필수 세그먼트
mydomain.com/profile
mydomain.com/profile/1234/something
는 제 2 포맷의 경우,
1234
는 필요한 정수 값이어야한다. 첫 번째 형식은 리터럴 경로를 사용하여 간단합니다. 두 번째 형식에 세그먼트 자식 경로를 추가 할 수 있다고 생각했지만 작동하도록 설정할 수 없습니다. 첫 번째 형식을 생략하고 세그먼트 경로가있는 두 번째 형식 만 시도했지만 그 중 하나에서 성공하지 못했습니다. 여기
내가 뭘하려 :
'profile' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/profile',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'profile'
)
),
'child_routes' => array(
'profile_view' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:code[/:username]',
'constraints' => array(
'code' => '[0-9]*',
'username' => '[a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'view_profile'
)
)
)
)
)
mydomain.com/profile
를 들어, 나는 다음과 같은 errror를 얻을 :
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'No RouteMatch instance provided'
mydomain.com/1234/something
, 나는 다음과 같은 오류 얻을 :
Fatal error: Uncaught exception 'Zend\Mvc\Router\Exception\InvalidArgumentException' with message 'Missing parameter "code"'
을 manual states the following :
If a segment is optional, it should be surrounded by brackets. As an example, “/:foo[/:bar]” would match a “/” followed by text and assign it to the key “foo”; if any additional “/” characters are found, any text following the last one will be assigned to the key “bar”.
그게 내가하는 일이 아닙니까? 제약 조건을 주석으로 처리하면 위의 오류가 그대로 유지됩니다.
무엇이 여기에 있습니까? 미리 감사드립니다.
기본 매개 변수를 추가하면 기본값을 통해 코드 값을 제공하기 때문에 작동합니다. assemble 메서드를 호출 할 때 제공해야합니다. – DASPRiD
흥미롭게도, 내 컨트롤러에서'code' 매개 변수를 얻을 수 있으며, 기본값 (0)이 아닙니다. 오히려, URL에 쓰는 값입니다. 어쨌든 제가 원했던 값입니다. – Andy0708
이것은 답변으로 표시하면 안됩니다. 그것은 단지 밴드 원조입니다. 나는 아래의 해답이라고 생각하는 것을 올렸다. 그것을보십시오. – Bluebaron