gpt4 book ai didi

php - 在 CakePHP 2.x 中使用命名参数

转载 作者:可可西里 更新时间:2023-10-31 23:31:30 25 4
gpt4 key购买 nike

我有以下方法可以查询来搜索我的笔记:

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 - 加载笔记的第 1 页

domain.com/notes/page/2 - 加载笔记的第 2 页

domain.com/notes/search/Hello - 在笔记中搜索 Hello

domain.com/notes/search/Hello/page/2 - 显示上述搜索的第 2 页

View 中的分页器如下所示:

<?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 } ?>

对于索引方法它工作正常,但是对于搜索方法它变得困惑,因为当我进行搜索时它没有像预期的那样将寻呼机与路由匹配。例如,我得到像 domain.com/notes/search/2/:q

这样的网址

此外,我真的不喜欢将分页器选项包装在 if 语句中,所以如果我能让它自动找出 url,那会很棒,因为这样做很麻烦,而且似乎是以上问题。

我已经在路由器的顶部连接了命名参数,如下所示:

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

最佳答案

最后,我选择使用 POST 而不是 GET 来进行搜索,这样一切都在服务器端处理,而不是通过凌乱的 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 中使用搜索方法以外的任何其他页面,我会清理 session :

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 - 在笔记中搜索 Hello(存储在 session 中)

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

关于php - 在 CakePHP 2.x 中使用命名参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20227026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com