2013-11-26 42 views
3

我有以下的方法,這需要查詢來搜索我的筆記:在CakePHP的2.x中使用命名參數

function search($q = null) 
{ 
    if ($q == null) 
    { 
     $this->redirect(array('action' => 'index')); 
    } 

    $this->paginate = array(
    'limit'=>5, 
    'order'=>'Note.datetime DESC', 
    'conditions' => array(
     'OR' => array(
      'Note.title LIKE' => '%'. $q . '%', 
      'Note.content LIKE' => '%'. $q . '%' 
      ) 
     ), 
     'Note.status'=>1 
    ); 

    $this->set('notes', $this->paginate()); 

    $this->render('index'); 
} 

正如你可以看到它採用所謂的「Q」一個參數是用來查詢模型數據。

我已經迷上了這個路由器像這樣:

Router::connect('/notes', 
    array('controller'=>'notes','action'=>'index', 'page' => 1), 
    array(
     'pass' => array('page') 
    ) 
); 

Router::connect('/notes/page/:page', 
    array('controller' => 'notes', 'action' => 'index'), 
    array(
     'pass' => array('page'), 
     'page' => '[1-9]+' 
     ) 
); 

Router::connect('/notes/search/:page/:q', 
    array('controller'=>'notes','action'=>'search', 'page' => 1), 
    array(
     'pass' => array('page','q') 
    ) 
); 

Router::connect('/notes/search/:q/page/:page', 
    array('controller' => 'notes', 'action' => 'search'), 
    array(
     'pass' => array('page','q'), 
     'page' => '[1-9]+' 
     ) 
); 

這樣我應該是越來越網址,如:

domain.com/notes - loads page 1 of notes

domain.com/notes/page/2 - loads page 2 of notes

domain.com/notes/search/Hello - searches for Hello in notes

domain.com/notes/search/Hello/page/2 - shows page 2 of the above search

視圖中的尋呼機的樣子:

<?php if(isset($this->request->params['named']['q'])) { ?> 
<?php $this->Paginator->options(array('url'=>array('controller' => 'notes', 'action' => $action, 'q' => $this->request->params['named']['q']))); ?> 
<?php } else { ?> 
<?php $this->Paginator->options(array('url'=>array('controller' => 'notes', 'action' => $action))); ?> 
<?php } ?> 

它工作正常的索引方法,但是對於搜索方法時感到困惑,當我做它不匹配的尋呼機搜索與預期的路線。例如,我得到的url像domain.com/notes/search/2/:q

另外我真的不喜歡不得不將paginator選項包裝在if語句中,所以如果我可以自動找出url,那會很棒,因爲它很混亂要做到這一點,似乎是造成上述問題的原因。

我已經連接了命名參數在路由器的頂部像這樣:

Router::connectNamed(array('q')); 

回答

2

最終我選擇要使用的不是GET POST我的搜索工作,所以一切都處理的服務器端,而不是與亂七八糟的url重寫並試圖變得聰明。

這就是我所做的樣子格式如:

<?php echo $this->Form->create('search', array('url'=>array('controller'=>'notes','action'=>'search'),'class'=>'search')); ?> 
    <label class="placeholder" for="q">Search</label> 
    <?php if(isset($q)) { $term = $q; } else { $term = ''; } ?> 
    <?php echo $this->Form->input('q', array('label'=>false,'id'=>'q','value'=>$term)); ?> 
    <button type="submit" class="btn ss-icon ss-search"></button> 
<?php echo $this->Form->end(); ?> 

搜索方法:

function search() 
{ 
    if ($this->request->is('post')) { 

     $this->Session->write('q', $this->request->data['search']['q']); 

     $this->redirect(array('action' => 'search')); 

    } else { 

     $q = $this->Session->read('q'); 

     $this->paginate = array(
     'limit'=>5, 
     'order'=>'Note.datetime DESC', 
     'conditions' => array(
      'OR' => array(
       'Note.title LIKE' => '%'. $q . '%', 
       'Note.content LIKE' => '%'. $q . '%' 
       ) 
      ), 
      'Note.status'=>1 
     ); 

     $this->set('q',$q); 

     $this->set('action','search'); 

     $this->set('notes', $this->paginate()); 

     $this->render('index'); 

    } 

} 

和路線:

Router::connect('/notes/search', 
    array('controller'=>'notes','action'=>'search', 'page' => 1), 
    array(
     'pass' => array('page') 
    ) 
); 
Router::connect('/notes/search/page/:page', 
    array('controller' => 'notes', 'action' => 'search'), 
    array(
     'pass' => array('page'), 
     'page' => '[1-9]+' 
     ) 
); 

,如果任何我收拾會議其他頁面卻在AppController中使用搜索方法:

if(strpos($this->here, Router::url(array('controller'=>'notes','action'=>'search'))) === 0) { 

    //echo 'yes'; 

} else { 

    $this->Session->delete('q'); 

} 

它可以給我的網址,如:

domain.com/notes - 載荷第1頁的筆記

domain.com/notes/page/2 - 加載頁面的注意事項2

domain.com/notes/search - 在筆記搜索你好(存儲在會話)

domain.com/notes/search/page/2 - 顯示上述搜索的第2頁