Image link with $confirmMessage alert in Cakephp HTMLhelper - possible? - cakephp

Is it possible to create an image with a link that also has a pop up alert [$confirmMessage] using the html helper in CakePHP?
This is my current text link:
$this->Html->link('Clear list', array('controller' => 'items', 'action' => 'clearlist', $model['Model']['id']), array(), 'Clear list?')
This how the image helper creates images with links:
echo $this->Html->image("recipes/6.jpg", array( "alt" => "Brownies", 'url' => array('controller' => 'recipes', 'action' => 'view', 6)));
However this allows only an htmlattributes array as the arguements for the link.
The $confirmMessage alert is not an html attribute is it?
This is the code I tried:
echo $this->Html->link($this->Html->image("clearall.png", array("alt" => "Clear list")), array('controller' => 'items', 'action' => 'clearlist', $model['Model']['id']), array(), 'Clear list?');
However this code printed the correct html for my img as text:
<img src="/img/clearall.png" alt="Clear list" />
Do I have to give up on htmlhelper in this case?

CakePHP does do this with the Html helper and you were really close!
<?php echo $this->Html->link($this->Html->image('clearall.png', array(
'alt' => 'Clear list')
), array(
'controller' => 'items',
'action' => 'clearlist',
$model['Model']['id']
), array(
'escape' => false,
'confirm' => 'Clear list?'
)); ?>
You could also have done it without the helper like so:
<a href="/items/clearlist/<?php echo $model['Model']['id']; ?>"
onclick="return confirm('Clear list?');">
<img src="/img/clearall.png" alt="Clear list" />
</a>
Thanks to ADmad and rtconner for showing me this in IRC.

Related

url passing parameters are not displayed

In cake php layout/default.ctp
I created a menu bar but here I done link the URL with like this
<li class='active'> <?php //echo $this->Html->link('Psychiatrist', array('controller' => 'pages','action' => 'home','?' => array('id' => '1')));
echo $this->Html->link('Psychiatrist', array('controller' => 'pages', 'action' => 'home',5)); ?></li>
<!-- <li class='active'><a href='#'><span>Podiatrist</span></a></li>-->
<li class='active'> <?php echo $this->Html->link('Podiatrist', array('controller' => 'pages', 'action' => 'home',5)); ?></li></li>
It works fine, when we clicked on link url shows as
http:/Projects/DoctorSample/pages/home/5
controller /PagesController.php
class PagesController extends AppController {
public function home($id='null'){
if(isset($id)){
echo $id;
}
}
}
But in my display it shows
Error: The view for PagesController::display() was not found. but I done home.ctp in pages
The problem on your app/Config/routes.php file.
Delete this line from app/Config/routes.php -
Router::connect(
’/pages/*
’,
array(’controller’ => ’pages’, ’action’ => ’display’)
);

Unable to use HTML Tags inside HtmlHelper::link()

I have the following link to generate the following link with helpers:
<a href="/rooms" class="ln-room">
<span class="room-icon cls-icon"></span>
<span class="lable">rooms</span>
</a>
I cannot supply the span tags inside $combine, as HtmlHelper::link() calls htmlspecialchars_decode(), thus converting all html to characters.
echo $this->Html->link($combine, array(
'controller' => 'rooms',
'action' => 'index'
),
array('class' => 'ln-room')
);
If the only way is to create a custom helper, how can it be done?
echo $this->Html->link($combine, array(
'controller' => 'rooms',
'action' => 'index'
),
array(
'class' => 'ln-room',
'escape' => false // <--- THIS
)
);

How to pass pagination parameters with additional params cakephp?

I have a list of categories which displays the list of items under it.
Thus:
routes have:
Router::connect('/category/:slug', array('controller' => 'Product',
'action'=>'catview', 'slug'=> '[0-9a-zA-Z]+'));
Router::connect('/category/:slug/:page', array('controller' => 'Product',
'action'=>'catview','slug'=> '[0-9a-zA-Z]+','page'=>'[0-9]+'));
and
when I do this in results page, it just doesn't work:
<?php
$slug = $this->params['slug'];
$this->Paginator->options(array('url'=> array('controller' => 'Product',
'action'=>'catview','slug'=> $slug)));
echo $this->Paginator->prev('<< Show me previous', array('class'=>'prev'))
. $this->Paginator->next('Show me more >>', array('class'=>'next')); ?>
It does not change the results, it shows the same result as it does on page 1.
Any ideas where I am going wrong?
Thanks to AD7six for the reference.
My controller needs to have :
$this->paginate = array(
//other stuff here
'paramType' => 'querystring'
);
Followed by:
$this->Paginator->options(array('url'=> array('controller' => 'Product',
'action'=>'catview','slug'=> $slug),
'convertKeys' => array('page')));
in the view file

CakePHP html helper imgage links

In Cake, we can add url in image() like this:
$this->Html->image('image src...', array('url' => 'some address...');
and output is:
<img src="image src..." />.
How to add class and others attributes to a tag?
echo $this->Html->link(
$this->Html->image($imageSrc, array(
'class'=>'class_of_image',
'height' => '50',
'width' => '100',
'alt' => 'awesome close-up of me eating pizza'
)),
array(
'controller' => 'your_controller',
'action' => 'the_action'
),
array(
'class' => 'class_of_anchor',
'escape' => false //to allow the image tag within the link
)
);
Feel free to make any/all parts of this into a single line - but for StackOverflow reasons, it's easier to read like this.

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