span in link with class - cakephp

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)
); ?>

Related

Cakephp format url with image and title

i'm tring to change my link into a CakePhp format link. I want aso to add the class "ajax" in my link.
<li><?php echo $this->Html->link($this->Html->image('images/home.png'),'Accueil', array ('action'=>'index.php'),array('class'=>'ajax')); ?>
My original link :
<li><a href="index.php" id="visited"><span class="home">
<img src="<?php echo $this->webroot; ?>images/home.png" alt=""></span>Accueil</a></li>
Thanks
You can do this as mark said and additionally if you want to add the span tag,
<li><?php echo $this->Html->link($this->Html->tag('span', $this->Html->image('images/home.png'), array('class' => 'home', 'escape' => false)).' Accueil', array ('action'=>'index.php'),array('class'=>'ajax', 'escape' => false)); ?></li>
Hope it helps.
echo $this->Html->tag('li',$this->Html->link(
$this->Html->tag('span',
$this->Html->Image('images/home.png',array(
'alt'=>'','height'=>'100%','width'=>'100%'
)).'Accueil',
array('class'=>'home')
),
array(
'controller' =>'','action' => 'index.php'
),
array(
'class' = 'ajax'
)
)
)
I have written this for you to learning link & tag building in cakephp....i thinks it helpful for you...............

cakephp link not working

I am new in php and cakephp. Just trying to make simple navigation menu.
<li><?php
//pr($this->Html-);
echo $this->Html->link('Entertainment', array(
'controller' => 'UpcommingEntertainments',
'action' => 'index'
));
?></li>
its works fine if I am at www.example.com. The problem is if I am at /admin/* and i click this link, it takes me to www.example.com/admin/Entertainment, I want to go to www.wxample.com/Entertainment.
What should be my link code?
Please try:
echo $this->Html->link('Entertainment', array(
'controller' => 'UpcommingEntertainments',
'action' => 'index',
'admin' => false, // thats what you need
'plugin' => false, // could be helpful if you plan using plugins
));
I included the plugin parameter, because you could encounter the same issue, if you use plugins.
Hope that helps.
<li><?php
//pr($this->Html-);
echo $this->Html->link('Entertainment', array(
'admin' => false, // add this
'controller' => 'UpcommingEntertainments',
'action' => 'index'
));
?></li>
There is a good answer here offering a bit more indepth explanation.

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: creating a dropdown on homepge through an element problem

I try to create a search function on my homepage where the user can limit the search results by country.
It all works in my posts/index controller whereby the country list is automatically retrieved by a find('list).
However, on the homepage, the country dropdown remains empty. Below some code:
I try to retrieve the dropdown by using requestAction (please omit 'requestAction is slow from the comments, thanks)
homesearch.ctp ELEMENT:
<?php $this->requestAction('countries/getCountries');?>
<?php
echo $this->Form->create('Post', array(
'url' => array_merge(array('controller' => 'posts','action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('title', array('div' => false, 'empty' => true, 'label' => false));
echo $this->Form->input('country_id');
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>
getCountries function in countries controller:
function getCountries(){
$countries = $this->Country->find('list');
$this->set(compact('countries'));
}
Before diving into alternatives (loadmodule('Country') in PagesController etc), I think I am doing something wrong, there is no data flowing back from the requestAction function as debug taught me.
How do you guys wash this cow? Thanks!
... (please omit 'requestAction is slow from the comments, thanks)
For improved performance, replace:
<?php $this->requestAction('countries/getCountries');?>
with:
<?php $this->viewVars['countries'] = ClassRegistry::init('Country')->find('list'); ?>
This approach doesn't generate a second request.
function getCountries(){
$countries = $this->Country->find('list');
if (!empty($this->params['requested'])) {
return $countries;
} else {
$this->set(compact('countries'));
}
}
and in the element: <?php $countries = $this->requestAction('countries/getCountries');?>
man, it's right in the book: http://book.cakephp.org/view/991/requestAction

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