CakePHP Pagination with HTML Entities not working - cakephp

Could anyone riddle me this.
The following code produces this-> http://www.evernote.com/shard/s29/sh/87fce2c2-c323-461b-a5ac-1ccc6d2ba3ad/32b87ce0602a33dfda59c4b9e69be54b
<?php echo $this->Paginator->prev("‹", array('escape' => false), null, array('class' => 'pagination_disabled')); ?>
<?php echo $this->Paginator->numbers(array('separator' => '')); ?>
<?php echo $this->Paginator->next("›", array('escape' => false), null, array('class' => 'pagination_disabled')); ?>
I have cleared the cache and tried replacing the prev with an entity number and with the exact same code used in next, with the same result.
edit
Heh, just looked at it in IE, same issue but the buttons are reversed, the prev button renders fine but the next button prints the reference. Odd.

I had this exact problem and it drove me nuts.
The below appears to work fine:
echo $this->Paginator->prev('« ', array('escape'=>false), '« ', array('escape'=>false, 'class' => 'disabled'));
echo $this->Paginator->numbers(array('separator'=>' '));
echo $this->Paginator->next(' »', array('escape'=>false), ' »', array('escape'=>false, 'class' => 'disabled'));
I believe this behaviour is intended, but it doesn't seem very logical to me at least - let me know how you get on.
I'm not sure on the specifics, but both next and prev have a third parameter. In your code you have null - in my code I have the » - and my version displays fine.
string $disabledTitle optional NULL Title when the link is disabled.
NB in my app; my class="disabled" hid the &raquo from view - you might not want this.

The PaginatorComponent is expecting to receive 4 inputs:
Title when button is active;
Options when is active;
Title when button is disabled;
Options when is disabled;
You can put the 3rd element to null and it will take the title from when the button is active but you still need to specify it's options. So, you just need to add the option to disable escape on disabled button options, like this:
<?php echo $this->Paginator->prev("‹", array('escape' => false), null, array('class' => 'pagination_disabled', 'escape' => false)); ?>
<?php echo $this->Paginator->numbers(array('separator' => '')); ?>
<?php echo $this->Paginator->next("›", array('escape' => false), null, array('class' => 'pagination_disabled','escape' => false)); ?>

Related

cakephp $this->request-is("post") return false for just one form, so strange?

I have many forms in the website. They are all created in the similar way like
<?php echo $this->Form->create('SysUser');?>
<fieldset>
<legend><?php echo __('Edit Basic Information'); ?></legend>
<?php
echo $this->Form->input('SysUser.first_name');
echo $this->Form->input('SysUser.family_name',array('label'=>__("Last Name")));
echo $this->Form->input('SysUser.mobile_phone_number');
echo $this->Form->input('SysUser.user_name',array('label'=>__("Screen Name")));
echo $this->Form->input('action', array('type'=>'hidden','value'=>'edit_basic_info'));
echo $this->Form->input('SysUser.id', array('type'=>'hidden','value'=>$user["id"]));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
But the type of one form becomes "put" , not "post". I never explicitly set the type to "post" when I create these forms. I gather CakePHP sets the default value to post. Now it seems something wrong about the way I create this new special form. Oddly, this was working days ago!
I don't know what's wrong. Here is it:
<?php echo $this->Form->create('Member'); ?>
<fieldset>
<legend><?php echo __('Basic Profile Setup'); ?></legend>
<?php
echo $this->Form->input('Member.gender_id');
$w = array();
for ($i = 40; $i < 120; $i++) {
$w[$i] = $i . " kg";
}
$h = array();
for ($i = 120; $i < 230; $i++) {
$h[$i] = $i . " cm";
}
echo $this->Form->input('Member.height', array(
'options' => $h,
'empty' => __("choose one")
));
echo $this->Form->input('Member.weight', array(
'options' => $w,
'empty' => __("choose one")
));
$options['minYear'] = date('Y') - 78;
$options['maxYear'] = date('Y') - 18;
echo $this->Form->input('Member.birthdate', $options);
echo $this->Form->input('Member.residential_location_id', array('label' => __("City/Location")));
echo $this->Form->input('Member.occupation_id',array('id'=>'MemberOccupationId'));
echo $this->Form->input('action', array('type' => 'hidden', 'value' => 'create_member'));
?>
</fieldset>
<?php
echo $this->Form->end(array("label" => __('Save')));
When the Request data contains a Model.id CakeRequest::method() is set to put. The preferred way to handle this in cakephp would be as follows.
if ($this->request->is(array('post', 'put'))) {
// Code
}
You can see this in baked controller, edit actions.
Not sure why it is happening, but you can set the form type this way:
<?php echo $this->Form->create('Member', array('type' => 'post')); ?>
I had this problem as well. In my situation this was happening when I had validation errors. So for the second run, the script thought it was a PUT request instead of a POST request. Now, because it was a PUT, it didn't even get inside the if-clause where I checked if it was a POST, so it would return to the input and try to create a POST request. This was looping forever.
The solution? Checking for a NOT GET.
So you would get something like this:
if (!$this->request->is('get')){
//Save logic here
}
I have seen an example like this in the Cookbook, but I can not find it. So I have a feeling it has been updated, but as far as I am concerned you have to use this method. So you will cover a PUT, as well as a POST request.
UPDATE
It is not recommended to use this approach. It is a PUT/POST based on if the id is set in the form. Since I was setting the id based on the type of request, instead of if it actually exists, it was switching over and over again. I am using 1 form for the add and the edit action. They both use the edit.ctp which is just set up more flexible.
From the Cookbook:
If $this->request->data contains an array element named after the form’s model, and that array contains a non-empty value of the model’s primary key, then the FormHelper will create an edit form for that record.
Is that the case, perhaps? What's Member's primary key?
I had the same issue and after 4 hours searching I just resolved it appending the Model name to the fields in the view like this:
<?php echo $this->Form->create('User');?>
<?php
echo $this->Form->input('User.id');
echo $this->Form->input('User.username', array('readonly' => true));
echo $this->Form->input('User.email', array('readonly' => true));
echo $this->Form->input('User.name');
echo $this->Form->input('User.phone');
echo $this->Form->input('User.gender');
echo $this->Form->input('User.locale', array('id' => 'locale_select', 'options' => array('es' => __('Spanish'), 'en' => __('English'))));
echo $this->Form->input('User.birthday', array('type' => 'date', 'dateFormat' => 'DMY', 'minYear' => date('Y') - 100, 'maxYear' => date('Y')));
?>
<?php echo $this->Form->end(__('Save', true));?>
Well, I have to say that this code is in a plugin, so I don't know if there could be any other problems. But other forms in that plugin work perfect and this one needs to have the Model name.
One of the ways I've handled this situation is to create my own detector that defines the context of post OR put. This goes in the beforeFilter() method in AppController:
// add a simple form post detector
$this->request->addDetector('formPosted', array(
'env' => 'REQUEST_METHOD',
'options' => array('post', 'put')
));
Then when you need to check if a form has been posted (or "putted"), then:
if ($this->request->is('formPosted')) { ... }
Since the detector is added in AppController, the condition can be checked from within any controller method.

CakePHP Routes and Pagination

I have created a route which looks like this
Router::connect('/:slug', array('controller' => 'categories', 'action' => 'view'),
array('pass' => array('slug')));
Until here, everything works okey, visiting the link http://example.com/animals-and-pets, works perfect.
On this page I have a pagination and this gives me e big problem, the links for the pages, are generating wrong, like this: http://example.com/categories/view/animals-and-pets/page:2.
The result that I want to obtain is example.com/animals-and-pets/2.
Thanks for your help in advance!
I once did it this way:
change CakePhp1.3 paginator destination url?
However, it could get much easier if you use \page:2 instead of \2
Now in cake 2.0 I call the $this->Paginator->options() to set the correct url in the view before the rest of the pagination options. Something like:
//Set the correct url for the pagination, cake will add the "page:" and "sort:" variables to this url
$this->Paginator->options(array('url'=> array('controller' => 'categories', 'action' => 'view', 'slug' => $this->params['pass'][0])));
//now display the pagination
echo $this->Paginator->counter(array('format' => __('Page {:page} of {:pages}')));
echo $this->Paginator->prev('«', array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next('»', array(), null, array('class' => 'next disabled'));
Hope this helps

CakePHP: variables are cleared when next/privous is clicked

I have an action from a controller that provides a result variable via $this->set('found_products', $data);. The view page products.ctp is divided into two sections:
On top, a form where a user enters a string to search $found_products is set for the view.
Below it, paginated results that is displayed only if $found_products is set. i.e. if (isset($found_products)) is true.
When if (isset($found_products)) is true, I get the first page displayed below the form with the search string already in the text box. The URL for this is myapp/controller/products.
The problem occurs when I move into the next pages. The URL becomes myapp/controller/action/products:2 and none of the variables used under myapp/controller/products exist. It looks like moving onto a new page clears all variables.. Below is the code I'm using for paging, and I have no reroute rules written for this. How do I solve this issue?
<div class="paging">
<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
<?php echo $this->Paginator->numbers();?>
<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
</div>
I've tried to work around this using $_SESSION in the action (products) and setting this for the view, but when I did this, `$this->Paginator' no longer worked.
You can use the $this->Paginator->options to preserve the passed arguments in the pagination links. Try this code
<div class="paging">
<?php $this->Paginator->options(array('url' => $this->passedArgs)); ?>
<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
<?php echo $this->Paginator->numbers();?>
<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
</div>

span in link with class

I was messing around with the cakePHP link tag...
And, the span is located inside the link.
I know about escape => false, but seems to me, this doesn't really work.
The php part is embedded within the 'li' as below:
<?php echo $this->Html->link($html->tag('span','Hello World'),
array('controller'=>test,
'action'=>index),
array('class' => 'class_b'),
array('escape' => false)
)
?>
My problem here is, the 'span' tag isn't 'eliminated' from the view. What am I doing wrong?
Thanks.
It's rather simple
<?
echo $this->Html->link($this->Html->tag('span',__('News',true)),array('controller'=>'news','action'=>'index'),array('escape'=>false,'class'=>'news'));
?>
you just need to add third parameter to Html link escape=>false
I think this is what you might be looking for, then:
<?php
echo $this->Html->link(
$this->Html->tag('span', 'Hello World.', array('class' => 'class_b')),
array(
'controller' => 'test',
'action' => 'index'
)
);
?>
Found in this reference (near the bottom of the page):
http://book.cakephp.org/1.3/view/1442/link
I was searching answer and for me works this piece of code:
<?php echo $this->Html->link(
$this->Html->tag('span', 'Hello world', array('class' => 'class_a')),
array('controller' => 'test', 'action' => 'index'),
array('escape' => FALSE)
); ?>

CakePHP Canonical Tag with html helper

How can I create this using the html helper? (with inline=false so i can specify it on a per-view basis)
<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish" />
Can't seem to find anything on this, apart from a patch that doesn't work.
Found this in CakePHP bugtracking site : http://cakephp.lighthouseapp.com/projects/42648/tickets/1063-support-for-custom-meta-tag-elements-in-htmlhelper
Apparently you can use
echo $this->Html->meta('canonical', 'http:://example.com', array('rel'=>'canonical', 'type'=>null, 'title'=>null));
//outputs <link href="http:://example.com" rel="canonical" />
It seems my friend just told me that I told him how to do this a few months back, problem solved...
<?php echo $this->Html->meta('canonical',
'http://www.example.com/product.php?item=swedish-fish',
array('rel'=>'canonical', 'type'=>null, 'title'=>null, 'inline' => false)
);?>
If you're looking for something that automatically outputs the current url into a canonical tag, you can use the $this->Html->url(null, true); or $this->here; within the Cakephp html helper.
<?php echo $this->Html->meta('canonical', $this->Html->url(null, true), array('rel'=>'canonical', 'type'=>null, 'title'=>null)); ?>
Or
<?php echo $this->Html->meta('canonical', $this->here, array('rel'=>'canonical', 'type'=>null, 'title'=>null)); ?>
WARNING:
I have heard of some cases where $this->here has issues on local dev environments.
In CakePHP 2:
echo $this->Html->meta('canonical', 'http://example.com', array('rel' => 'canonical', 'type' => null, 'title' => null, 'inline' => false));
In CakePHP 3:
echo $this->Html->meta('canonical', 'http://example.com', array('rel' => 'canonical', 'type' => null, 'title' => null, 'block' => true));
Note that the main difference between versions is that CakePHP 2 uses 'inline' => false whereas CakePHP 3 uses 'block' => true to place these within the document <head> tags.
In CakePHP 4:
In your view (es: Articles/view.php) add this:
<?php $this->Html->meta(
'canonical',
Router::url(['controller' => 'Articles', 'action' => 'view', $article->slug], true),
[
'block' => true
]
);
?>
Then you print it in your layout/default.ctp with this instruction
<?= $this->fetch('meta') ?>

Resources