我正在创建一个使用CakePHP分页的图库。问题是当我点击任何页码链接时,我得到这样的内容:http://localhost/mySite/galleries/displayImages/page:2/sort:GalleryImage.modified/direction:desc
,当我希望它只显示http://localhost/mySite/Gallery/(pagenumber)
时。如何解决这个问题?
我正在使用CakePHP 2.4.4。
routes.php文件
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
Router::connect('/Gallery', array('controller' => 'galleries', 'action' => 'displayImages'));
Router::connect('/Gallery/:page', array('controller' => 'galleries', 'action' => 'displayImages'),array('page' => '[0.9]+'));
Router::connect('/Videos', array('controller' => 'galleries', 'action' => 'displayVideos'));
Router::connect('/Contactos', array('controller' => 'contacts', 'action' => 'contactUs'));
Router::connect('/Posts', array('controller' => 'posts', 'action' => 'index'));
Router::connect('/Posts/add', array('controller' => 'posts', 'action' => 'add'));
Router::connect('/Posts/edit', array('controller' => 'posts', 'action' => 'index'));
Router::connect('/Posts/view', array('controller' => 'posts', 'action' => 'view'));
Router::connect('/Posts/delete', array('controller' => 'posts', 'action' => 'delete'));
Router::connect('/Login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/Logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/Users', array('controller' => 'users', 'action' => 'index'));
Router::connect('/Users/add', array('controller' => 'users', 'action' => 'add'));
Router::connect('/Users/edit', array('controller' => 'users', 'action' => 'edit'));
Router::connect('/Users/delete', array('controller' => 'users', 'action' => 'delete'));
Router::connect('/Admin/Dashboard', array('controller' => 'users', 'action' => 'adminDashboard'));
Router::connect('/Admin/Dashboard/Gallery/Upload', array('controller' => 'galleries', 'action' => 'uploadImages'));
Router::connect('/Admin/Dashboard/Gallery/Delete', array('controller' => 'galleries', 'action' => 'deleteImages'));
Router::connect('/Admin/Dashboard/Videos/Upload', array('controller' => 'galleries', 'action' => 'uploadVideos'));
Router::connect('/Admin/Dashboard/Videos/Delete', array('controller' => 'galleries', 'action' => 'deleteVideos'));
AppController beforeFilter
public function beforeFilter() {
if (isset($this->request->params['page'])) {
$this->request->params['named']['page'] = $this->request->params['page'];
}
$this->Auth->allow('index', 'view','displayImages','image','ContactUs','Contact','displayVideos');
}
控制器
public $components = array('Paginator');
public $paginate = array('limit' => 15, 'order' => array('modified' => 'desc'), 'contain' => array('GalleryImage', 'GalleryVideo'));
public function displayImages(){
$this->set('title_for_layout', 'Galeria de Fotografias');
$this->layout = 'default';
$this->loadModel('GalleryImage');
$this->Paginator->settings =$this->paginate;
$gallery_images=$this->Paginator->paginate('GalleryImage');
//$gallery_images = $this->GalleryImage->find('all');
$this->set('gallery_images', $gallery_images);
//$image_display = $gallery_image['path']
}
查看
<style>
h3{
text-align: left;
}
</style>
<h3>Galeria</h3>
<br>
<table width="100%">
<tr>
<?php
$i=0;
foreach( $gallery_images as $gallery_image ):?>
<td align="center" class="thumbnail" style="display:inline-block;">
<?php
$src =$this->webroot. 'img/Gallery/'.$gallery_image['GalleryImage']['name'];
echo "<a href=\"".$src. "\" rel=\"lightbox\">".$this->Timthumb->image('/img/Gallery/' . $gallery_image['GalleryImage']['name'] , array('width' => 267, 'height' => 189))." </a>";
?>
</td>
<?php $i++;
if($i==3){
echo "</tr><tr>";
$i=0;
}
?>
<?php endforeach ?>
</tr>
</table>
<div class="pagesDiv">
<ul class="pagination">
<li><?php echo $this->Paginator->first(__('Primeira', true), array());?></li>
<li><?php echo $this->Paginator->numbers(array('separator' => '' ,'currentTag'=>'span' ,'class' => 'numbers', 'first' => false, 'last' => false));?></li>
<li><?php echo $this->Paginator->last(__('Última', true), array('class' => 'disabled'));?></li>
</ul>
</div>
答案 0 :(得分:2)
指定routes.php中的路径
Router::connect('/Gallery/:page', array('controller' => 'galleries', 'action' => 'displayImages'),array('page' =>'[0-9]+'));
并在AppController.php中添加这段代码
public function beforeFilter() {
if (isset($this -> request -> params['page'])) {
$this -> request -> params['named']['page'] = $this -> request -> params['page'];
}
}