How to customize CakePHP pagination url? - cakephp

currently i am using cakephp version 2.5.3
I want to change my cakephp pagination url.
My current URL is http://www.example.com/newsFeeds/ajax_news_feed/page:2 I need http://www.example.com/newsFeeds/index/page:2
My Code:
<?php
echo $this->Paginator->prev(' <<' . __('Previous '), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers();
//echo $this->Paginator->url(array('controller'=>'newsFeeds', 'action'=>'index'));
//echo $this->Paginator->link('Sort by title on page 5', array('controller'=>'newsFeeds', 'action'=>'index'));
echo $this->Paginator->next(__(' Next') . '>> ', array(), null, array('class' => 'next disabled'));
?>
Above pagination is showing-
When i am clicking 2 then the link is going to http://www.example.com/newsFeeds/ajax_news_feed/my_post/page:2 but i need http://www.example.com/newsFeeds/index/my_post/page:2
Please tell me how to change controller and action in pagination?

User $this->Paginator->options-
Code:
<?php
$this->Paginator->options['url'] = array('controller' => 'newsFeeds', 'action' => 'index/my_post');
echo $this->Paginator->prev(' <<' . __('Previous '), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers();
echo $this->Paginator->next(__(' Next') . '>> ', array(), null, array('class' => 'next disabled'));
?>

For cakephp 3 its a little bit different:
$this->Paginator->options([
'url' => [
'controller' => 'newsFeeds',
'action' => 'index',
'my_post']
]);

Related

How to implement pagination with search in CakePHP

My pagination is working, even the search is also working. But the problem that I'm having is when i click on next page link in the pagination links. The search is not working for the next page of the pagination. Also I need to know how I send other parameters through the url, and use them in query of pagination. I need help on this as I am novice in CakePHP.
In controller page I have used this code:
class StatesController extends AppController {
public $components = array('Paginator');
public $paginate = array(
'limit' => 2,
'fields' => array('State.id', 'State.state','State.code'),
'order' => array(
'State.state' => 'asc'
)
);
public function admin_index() {
$this->layout = false;
$this->layout = 'adminlayout';
//****** pagination starts
$search=$this->request->data('State.search');
$this->Paginator->settings = $this->paginate;
// similar to findAll(), but fetches paged results
$stateListAr = $this->Paginator->paginate('State',
array('State.state LIKE' => "%".$search."%")
);
$this->set('stateListAr', $stateListAr);
//****** pagination ends
$this->set('stateListAr',$stateListAr);
$this->render('admin_index');
}
}
In view page I have used this code:
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(array('first' => 'First page')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
i have got solution to my problem , here it is . I have used a code in view page , here $search variable i have setted data from controller.
$search);
$this->Paginator->options(array(
'url' => $urlParamAr
));
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->numbers(array('first' => 'First page'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
?>

how to make the paginator style clickable?

I've added a class to style the Paginator to make it look like a button, but the only clickable area is the text inside , is there any way to fix that ?
<?php echo $this->Paginator->next('next >>', array('class'=>'Paginator')); ?>
if you use the cake.generic.css by default this is the form to make this.
<?php
echo "<div class='paging'>";
echo $this->Paginator->prev('<< ' . __('anterior ', true), array(), null, array('class'=>'disabled'));
echo $this->Paginator->numbers(array( 'class' => 'numbers' ));
echo $this->Paginator->next(__(' siguiente', true) . ' >>', array(), null, array('class' => 'disabled'));
echo "</div>";
?>

Ajax-pagination triggers only once

i implemented the pagination-components according to the official documentation:
http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
I also included ajax-support. Basically everthing works fine except one thing: If i switch from page 1 to page 2 the ajax-request will work, after that i switch from page 2 to 3 instead a ajax-request a normal Post-Request will be performed. So the ajax-thing works only once.
My controller looks like this (short form):
public $components = array('Paginator', 'RequestHandler');
public $helpers = array('Js', 'Categorie');
....
$this->Paginator->settings = array(
'limit' => '15'
);
No the more important thing is my view: (jquery are included in my layout...)
$this->Js->JqueryEngine->jQueryObject = 'jQuery';
$this->Paginator->options(array(
'update' => '#paginate',
'evalScripts' => true,
'before' => $this->Js->get('#busy-indicator')->effect('fadeIn', array('buffer' => false)),
'complete' => $this->Js->get('#busy-indicator')->effect('fadeOut', array('buffer' => false))
));
?>
<?php
echo $this->Html->image('indicator.gif', array(
'id' => 'busy-indicator'
));
?>
<div id="paginate">...display data...</div>
<?php
echo $this->Paginator->numbers();
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
echo $this->Paginator->counter();
?>
That's all... as mentioned before: Switch Page 1->2 will work, Switch Page 2-3 will do a normal POST-Request, Switch 3-4 will work and so one...
A bit strange, so any idea? Btw. i tried different jquery-versions without any effect (currently i am using 2.0.3...)
Thanks - Best regards
Teyhouse
In this case, you just needed to also have:
<?php echo $this->Js->writeBuffer(); ?>
In your app/View/Layouts/ajax.ctp file.

CakePHP Form Action Button

I am trying to place a non form related button into the view for a registration button to direct people to a controller action but i continue to get error500 internal error. Any ideas what i am doing wrong here?
<?php
echo $this->Form->create('User');
echo $this->Session->flash();
echo $this->Form->input('username', array('label' => false, 'div' => false, 'class' => 'w-icon validate[required]'));
echo $this->Form->input('password', array('label' => false, 'div' => false, 'class' => 'w-icon validate[required]'));
echo $form->button('Register', array('type' => 'button', 'class' => 'button red tiny'));
echo $this->Form->submit('Login', array('class' => 'button blue tiny'));
echo $this->Form->end();
?>
$form->button is CakePHP 1.2 syntax, $this->Form is 1.3 onwards.

How to call CSS class on a CakePHP Html->link?

I am a complete noob to programming and CakePHP all together, so please be patient. How am I supposed to call the CSS on this Html->link:
<?php echo $this->Html->link(__('Blogs', true), array('controller' => 'posts', 'action' => 'index') ); ?>
Please help.
Not sure what you mean by "call the css", I think you want to add a class to this link? IF that's the case you can just add another array as an argument and it will turn key =? value into HTML attributes. EG:
echo $this->Html->link(__('Blogs', true), array('controller' => 'posts', 'action' => 'index'), array('class' => 'my-class'));
This is all explained in the CakePHP docs.
Another Solution will be the inline style :
echo $this->Html->link(__('Blogs', true), array('controller' => 'posts', 'action' => 'index'), array('style' => 'color:red;font-size:20px;'));
Please try this CakePHP 3 in
<?php
echo $this->Html->link(__('Blogs'), ['controller' => 'posts', 'action' => 'index'], ['class' => 'mb-xs mt-xs mr-xs btn btn-warning']);
?>
I think this works:
<?php echo $this->Html->link('Add Task', array('action'=>'add'), array('class' => 'tac')); ?>

Resources