2012-10-08 2 views
1

나는 다음과 같은 경로를 연결 :CakePHP의 2.x에서 역방향 라우팅 링크 도우미

Router::connect('/:city/dealer/:id', 
    array('controller' => 'dealers', 'action' => 'view'), 
    array(
     'pass' => array('city', 'id'), 
     'city' => '[a-z]+', 
     'id' => '[0-9]+' 
     ) 
    ); 

이 잘 작동하고 수 : domain.com/washington/dealer/1

그러나 이 URL에 대한보기에서 적절한 HTML 링크를 생성하려면 어떻게합니까? 난 그냥 이렇게 할 경우 :

http://domain.com/dealers/view/washington/1

:

echo $this->Html->link(
    'Testlink', 
    array('washington', 'controller' => 'dealers', 'action' => 'view', 1) 
); 

그것은 생성 된 링크의 끝까지 모든 PARAMS 추가

어떻게하면 제대로 할 수 있습니까?

+0

'/를

Router::connect('/:city/dealer/:id', array('controller' => 'dealers', 'action' => 'view', 'id'=>':id'), array('pass' => array('city', 'id'), 'city' => '[a-z]+', 'id' => '[0-9]+' )); 

희망과 같은 경로를 만들/: 당신의 경로에 대한 'id'? 다른 컨트롤러가 동일한 패턴을 사용하도록 하시겠습니까? –

+0

'/ : city/: controller/: id'를 사용하면 Cake는 domain.com/washington/dealer/1에 대한 DealerController가 누락되었다고 불평합니다. 기본적으로이 컨트롤러가 App에 있습니다. – Sebastian

+0

try domain.com/ washington/dealer/1 (딜러의 공지 사항) –

답변

2

난 당신이 아직도처럼 PARAMS를 지정해야합니다 믿습니다

echo $this->Html->link('Testlink', 
    array('controller' => 'dealers', 'action' => 'view', 'city' => 'washington', 
                 'id'=> 1)); 

케이크는 요리 책에서 비슷한 예를 가지고 :

<?php 
// SomeController.php 
public function view($articleId = null, $slug = null) { 
    // some code here... 
} 

// routes.php 
Router::connect(
    '/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks 
    array('controller' => 'blog', 'action' => 'view'), 
    array(
     // order matters since this will simply map ":id" to $articleId in your action 
     'pass' => array('id', 'slug'), 
     'id' => '[0-9]+' 
    ) 
); 

// view.ctp 
// this will return a link to /blog/3-CakePHP_Rocks 
<?php 
echo $this->Html->link('CakePHP Rocks', array(
    'controller' => 'blog', 
    'action' => 'view', 
    'id' => 3, 
    'slug' => 'CakePHP_Rocks' 
)); 
+0

죄송합니다. 예제를 언급 했어야합니다. 문제는 LinkHelper가 여전히 매개 변수를 끝에 놓는다는 것입니다. 이것은/Cakes 예제에서 생성 된 링크입니다. http://domain.com/dealers/view/city:washington/id:1 – Sebastian

0

안녕하세요 세바스찬의 아마 당신을 도울 너무 늦게, 하지만이 문제로 다른 사람을 도울 수 있습니다. 문제를 해결하는 열쇠는 Helper 클래스의 url 메소드에 추가하는 것입니다. 내보기/도우미에서 AppHelper.php를 만들어이 작업을 수행했습니다. 이 모양입니다. 나는 당신 도시의 매개 변수를 변경했습니다. 도시/: 컨트롤러

보기/도우미/AppHelper.php

<?php 
App::uses('Helper', 'View'); 
class AppHelper extends Helper { 

    function url($url = null, $full = false) { 
      if (is_array($url)) { 
        if (empty($url['city']) && isset($this->params['city'])) { 
          $url['city'] = $this->params['city']; 
        } 

        if (empty($url['controller']) && isset($this->params['controller'])) { 
          $url['controller'] = $this->params['controller']; 
        } 

        if (empty($url['action']) && isset($this->params['action'])) { 
          $url['action'] = $this->params['action']; 
        } 
      } 

      return parent::url($url, $full); 
    } 

} 
?> 

그럼 난이 도움이 :)

사용하면 어떻게됩니까