2014-09-04 1 views
0

가 나는/파일 컨트롤러가 컨트롤러 액션에 URL 매개 변수를 얻을/추가하는 방법 : 업로드하고젠드 : 두 가지 작업을해야합니다

가 정의되어 다운로드 다음과 같이

'files' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => '/files[/:action]', 
     'defaults' => array(
      'controller' => 'Application\Controller\Files', 
      'action' => 'index', 
     ), 
    ), 
), 

내가 원하는/files/download/1? authString = asdf와 같이 액세스 할 다운로드 작업. 이 경우 1은 fileId입니다.

경로를 설정하려면 /files[/action[/:fileId]] 경로를 변경할 수 있음을 이해합니다. 제가 틀렸다면 수정 해주세요. 그렇다면 downloadAction 내부에서 fileId에 액세스하는 방법은 무엇입니까? 그리고 경로 정의가 작동하도록 변경해야 할 것이 있습니까?

답변

2

난 그냥 당신이 잘못 아니에요

틀렸다면 정정 해줘, 경로를 설정하는 /files[/action[/:fileId]]에 경로를 변경할 수 있습니다, 그것은 유효한 경로가 될 것입니다.

내가 경로 정의를 변경해야 할 것이 있습니까? 당신이 선택 경로 PARAM로 fileId을 추가하는 경우

은 당신이 설정되어 있는지 확인하기 위해 downloadAction() 내 일부 수동 검사를 수행해야합니다.

다른 해결책은 경로를 자식으로 분리하는 것입니다. 이렇게하면 각 경로에 올바른 매개 변수가 없으면 일치하지 않게됩니다.

'files' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => '/files', 
     'defaults' => array(
      'controller' => 'Application\Controller\Files', 
      'action' => 'index', 
     ), 
    ), 
    'may_terminate' => true, 
    'child_routes' => array(

     'download' => array(
      'type' => 'Segment', 
      'options' => array(
       'route' => '/download/:fileId', 
       'defaults' => array(
        'action' => 'download', 
       ), 
       'constraints' => array(
        'fileId' => '[a-zA-Z0-9]+', 
       ), 
      ), 
     ), 

     'upload' => array(
      'type' => 'Literal', 
      'options' => array(
       'route' => '/upload', 
       'defaults' => array(
        'action' => 'upload', 
       ), 
      ), 
     ), 

    ), 
), 

어떻게 내가) 가장 쉬운 방법은 경로에서 매개 변수를 가져 use the Zend\Mvc\Controller\Plugin\Params controller plugin에있을 것 downloadAction

내부 fileId에 액세스 않습니다. 아주 명확하고 완전한 답변에 대한 경로에서

// FilesController::downloadAction() 
$fileId = $this->params('fileId'); 

또는 특별히

// FilesController::downloadAction() 
$fileId = $this->params()->fromRoute('fileId'); 
+0

감사합니다. – Bogdan