2013-06-25 2 views
0

현재 세그먼트 경로가 다음과 같습니다 : /shop/:shopId/shopId에는 기본값이 없습니다.ZF2 라우터에 기본 값을 입력하십시오 라우터

경로가 일치 할 때마다 Module.php의 코드가 실행되고 shopId에 따라 준비 작업을 수행하고 예를 들어 세션에 저장합니다.

내 질문에, 가능하다면 그 시점에서 경로의 기본값을 shopId으로 설정 하시겠습니까? 최종 목표는이 순간부터 매번 shopId을 지정하지 않고 URL을 조합 할 수있게하는 것입니다.

ZF1에서이 동작은 기본적으로 요청에서 일치하는 매개 변수가 URL을 어셈블 할 때 재사용되고 사용자가 명시 적으로 제거하도록 지정해야한다는 것을 기억합니다. 이제는 동일한 기능이 필요하지만 모든 assemble() 호출을 다시 작성하지 않고 Module.php 레벨로 구성했습니다.

답변

1

옵션 하나 : 당신의 indexAction에서

$id = $routeMatch->getParam('id', false); 
if (!$id) 
    $id = 1; // id was not supplied set default one note this can be added as constant or from db .... 

옵션이 : 당신이 컨트롤러 module.config.php

'product-view' => array(
       'type' => 'Literal', 
       'options' => array(
        'route' => '/product/view', 
        'defaults' => array(
         'controller' => 'product-view-controller', 
         'action'  => 'index', 
        ), 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        'default' => array(
         'type' => 'Segment', 
         'options' => array(
          'route' => '[/:cat][/]', 
          'constraints' => array(
           'cat'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
          ), 
          'defaults' => array(
          ), 
         ), 
        ), 
       ), 
      ), 

의 설정을 제공

public function indexAction() 
    { 
     // get category param 
     $categoryParam = $this->params()->fromRoute('cat'); 
     // if !cat then get random category 
     $categoryParam = ($categoryParam) ? $categoryParam : $this->categories[array_rand($this->categories)]; 
     $shortList = $this->listingsTable->getListingsByCategory($categoryParam); 
     return new ViewModel(array(
      'shortList' => $shortList, 
      'categoryParam' => $categoryParam 
     )); 
    }