4

아래 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”.

그게 내가하는 일이 아닙니까? 제약 조건을 주석으로 처리하면 위의 오류가 그대로 유지됩니다.

무엇이 여기에 있습니까? 미리 감사드립니다.

답변

4

나는 더 많이 가지고 놀았습니다. 디버깅 후, 나는 아직도 그것을 알아낼 수 없었다. code 매개 변수에 기본값을 추가하려고 시도했지만 트릭을 수행하는 것처럼 보였습니다. 왜 작동하는 지구에서 그리고 어떻게 의미가 있는지, 나는 모른다. URL에 매개 변수의 URL 값을 제공 한 경우 기본 값을 지정했는지 여부는 중요하지 않습니다. 그럼에도 분명히 문제는 중요합니다. 적어도이 경우에는 그렇습니다.

다음은 작동 코드입니다. 또한 code 매개 변수에 대한 정규 표현식을 업데이트했습니다.

'profile' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal', 
    'options' => array(
     'route' => '/profile', 
     'defaults' => array(
      'controller' => 'User\Controller\User', 
      'action'  => 'profile', 
     ), 
    ), 
    'may_terminate' => true, 
    'child_routes' => array(
     'view' => array(
      'type' => 'Zend\Mvc\Router\Http\Segment', 
      'options' => array(
       'route' => '/:code[/:username]', 
       'constraints' => array(
        'code' => '\d+', 
        'username' => '[a-zA-Z0-9_-]*', 
       ), 
       'defaults' => array(
        'controller' => 'User\Controller\User', 
        'action' => 'viewProfile', 
        'code' => 0, 
       ), 
      ), 
     ), 
    ), 
), 
+0

기본 매개 변수를 추가하면 기본값을 통해 코드 값을 제공하기 때문에 작동합니다. assemble 메서드를 호출 할 때 제공해야합니다. – DASPRiD

+0

흥미롭게도, 내 컨트롤러에서'code' 매개 변수를 얻을 수 있으며, 기본값 (0)이 아닙니다. 오히려, URL에 쓰는 값입니다. 어쨌든 제가 원했던 값입니다. – Andy0708

+0

이것은 답변으로 표시하면 안됩니다. 그것은 단지 밴드 원조입니다. 나는 아래의 해답이라고 생각하는 것을 올렸다. 그것을보십시오. – Bluebaron

0

나는 당신이 그렇게처럼 may_terminate 옵션을 누락 생각 :

'profile' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal', 
    'options' => array(
     'route' => '/profile', 
     'defaults' => array(
      'controller' => 'User\Controller\User', 
      'action' => 'profile' 
     ) 
    ), 
    'may_terminate' => true, 
    'child_routes' => array(
    ... 
+0

불행하게도이 도움이되지 않는'mydomain.com/profile' 작동하지만'mydomain.com/프로필 /','mydomain.com/profile./1234'와'mydomain.com/1234/something' 모두 나에게 오류를 표시합니다 'code' 매개 변수가 누락되었습니다. – Andy0708

+0

'mydomain.com/profile /'을 만족시키는 경로가 없기 때문에 하위 경로에 당신을 넣고': code'가 필요하게됩니다. 제약 조건이': code'에서 제거되면이 경로가 작동합니까? 그렇지 않으면 나는': username'을 비활성화시키고'/ profile/1234'와'/ profile'로 작업하는 경로를 얻으려고합니다. – DrBeza

+0

네,'mydomain.com/profile /'이 작동 할 것으로 기대하지는 않았지만 필요한 매개 변수가 제공되지 않았을 때 자식 경로와 일치하는 이유가 궁금했습니다. 어쨌든, 제약 조건을 제거해도 차이는 없습니다. 경로에서 사용자 이름을 완전히 삭제해도 차이는 없습니다. 여전히 동일한 오류 메시지가 나타납니다. 나는 다른 모든 경로를 확인했는데 그 중 누구도 내 새로운 경로를 방해해서는 안됩니다. 나는 일부 결과를 없애기 위해 노력했지만 동일한 결과를 얻었습니다. 그것은 어쨌든 올바른 경로와 일치하므로 문제가되지 않습니다. 지금까지 도움을 주셔서 감사합니다. – Andy0708

1

당신이 Missing parameter "code"을 얻을 두 번째 예외는 아닌 조립 일치에서 비롯됩니다. 즉, 경로 profile/profile_view을 어셈블 할 때 코드를 제공하지 않았습니다.

그런데 부모 경로 이름 앞에 child_routes를 붙일 필요는 없습니다. 하위 경로 "보기"를 호출하고 profile/view으로 어셈블하십시오.

+0

조립과 일치하지 않는 오류가 맞습니다. 그러나, 그것은 여전히 ​​나에게 이해가되지 않는다. 그럼에도 불구하고 문제가 무엇인지 파악한 것 같습니다 (이해할 수는 없지만). code 매개 변수에 기본값을 추가하면 모든 것이 작동하는 것처럼 보입니다. 이상한! 또한 이름 붙이기에 대해 감사드립니다. 실제로 나는 그 이름을 그렇게 부르는 우연의 일치였습니다. :) – Andy0708

0

빗 타만에 따르면 두 가지 이유가 있습니다. 주된 이유는, 그는 말한다이다 :

<Bittarman> ezio, that looks like what happens when you make a new phprenderer rather than using the configured one 

내가 분명히 이유 중 적은 있었다 : 내 레이아웃> URL을() $ this-를 사용했다. 경로 일치가 없었기 때문에 (404 오류) 404 오류가 모두 발생했습니다.

<Bittarman> for all of those things, stop doing if (!$this->url() == $this->url('home') etc 
<ezio> ummm when it's home my business partner wants the whole order changed so google can hit on a bunch of keyords 
<Bittarman> instead, in the VIEW for home, set what you need. 
<ezio> how would i set the second example where i'm trying to highlight the currently-being-used menu item 
<Bittarman> so, in your layout, you just want echo $this->headTitle()->setSeparator(' - ')->setAutoEscape(false) 
<Bittarman> ezio, use the placeholder view helper 
<ezio> okay 
<ezio> thanks Bittarman ... tearing my hair out is not fun when you're already losing so much of it :p 
<Bittarman> <?= $this->placeholder('currently-being-used') ?> 
<Bittarman> and in your view, <?php $this->placeholder('currently-being-used')->append('foo') ?> 
<ezio> sense making a lot you are 
+0

이것이 OP에 어떻게 대답합니까? –

4

내 솔루션 : 빈 문자열에 선택적 매개 변수에 대한 기본 설정 나는 비슷한 문제로 실행

, 당신의 경우에 (선택적 매개 변수의 기본 값을 설정하여 그것을 해결 " ""빈 문자열로 '사용자 이름, 내 솔루션 :

'users' => array(
    'type' => 'segment', 
    'options' => array(
     'route' => '/users[/:user_id]', 
     'constraints' => array(
      'user_id' => '[0-9]*', 
     ), 
     'defaults' => array(
      'controller' => 'My\Controller\User', 
      'user_id' => "" 
     ) 
    ),