2012-02-29 3 views
2

나는이 내 응용 프로그램에서 다음과 같은 두 가지 경로 :라우팅이 작동하지

Router::connect('/posts/:id', 
        array('controller' => 'posts', 'action' => 'view'), 
        array('id' => '[A-Za-z0-9\._-]+', 'pass' => array('id'))); 

    Router::connect('/posts/:slug-:id', 
        array('controller' => 'posts', 'action' => 'view'), 
        array(
         'id' => '[A-Za-z0-9\._-]+', 
         'slug' => '[A-Za-z0-9\._-]+', 
         'pass' => array('id', 'slug') 
        )); 

및 URL의 예는 다음과 같습니다

/posts/This_is_the_first_post-1

그것이 보여 주 시겠어요 그러나 404 그러나 내가 /에 이드 전에 - URL을 변경하면 작동 할 것입니다 :/어떤 아이디어가 문제입니까? 그것을 일으키는 정규식인가요 ??

function view ($id = null, $slug = null) 
    { 
     $post = $this->Post->find('first',array('contain'=>array('User'=>'Profile'),'conditions'=>array('Post.id'=>$id))); 

     if (!$post) 
     { 
      throw new NotFoundException('404'); 
     } 
     else if($post['Post']['status'] == '0') // 0=draft 1=closed 2=open 
     { 
      if($post['Post']['user_id'] == $this->Auth->user('id')) 
      { 
       $this->Session->setFlash('Your post has NOT been published yet'); 
      } 
      else 
      { 
       throw new NotFoundException('404'); 
      } 
     } 

     if (Inflector::slug($post['Post']['title']) != $slug || $slug = null) 
     { 
      $this->redirect(array('id'=>$post['Post']['id'],'slug'=>Inflector::slug($post['Post']['title']))); 
     } 

     $this->set(compact('post')); 
    } 

답변

2

그것은 정규 표현식에 의한 것으로 보이는 :

다음은 뷰에 대한 나의 방법이다. 첫 번째 경로에서는 -을 허용하므로 두 번째 경로와 구별되지 않을 수 있습니다. :id-을 따라야합니다. 그것에 대해

Router::connect('/posts/:id', 
       array('controller' => 'posts', 'action' => 'view'), 
       array('id' => '[A-Za-z0-9\._]+', 'pass' => array('id'))); 
       //---Removed hyphen-----^^^^^^ 

Router::connect('/posts/:slug-:id', 
       array('controller' => 'posts', 'action' => 'view'), 
       array(
        'id' => '[A-Za-z0-9\._]+', 
       //---Removed hyphen-----^^^^^^ 
        'slug' => '[A-Za-z0-9\._]+', 
       //---Removed hyphen-----^^^^^^ 
        'pass' => array('id', 'slug') 
       )); 
+0

건배 :) – Cameron

+0

나는 실제로 질문을하기 전에 하이픈에게 자신을 제거하려했지만 여전히 파산 : 제거 잘못된 또는 무언가의 /해야합니다. 다시 건배. – Cameron

관련 문제