简体   繁体   中英

CakePHP Pagination - how to remove “page:” from url for better seo/cleaner URL

When I use CakePHP Paging I get an url like this:

http://example.com/php/page:2

What do I have to change in the controller, the view and the routes.php to create a working url like this:

http://example.com/php/2

Oh yes, now I see your question. Well you could do something like:

function index($page){
  $this->paginate = array('page'=>$page);
  $this->set('stuff', $this->paginate('YourControllerName'));
}

See here for more details: http://bakery.cakephp.org/articles/view/basic-pagination-overview-3

Also, of course you should do some validation that the page is an actual number and that the page would even exist but that is the basics of it i think.

About the routes and views, I have never tried but have a look at these posts on the cake groups, I think they have a problem similar to yours.

http://www.mail-archive.com/cake-php@googlegroups.com/msg45878.html

My guess is that this won't be easy to automate, you'll definitely need to do some tweaking.

For starters, you'll probably have to create your own paginator helper and inherit the default one. By the looks of the code, you'll need to override the link-generating code in PaginatorHelper::__pagingLink(), but probably numbers() and prev() etc.. since they all create links with the page param.

Maybe a better way would be to override your AppHelper::url(), check for the "page" param there and modify the url to accomodate your needs.

But, I haven't tried all this, so no guarantees..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM