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

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

Related

cakephp 2.5 Router - part of language string in URL

CakePHP Version 2.5.2
Config.Language 'spa'
If i'm in
mydomain.com/backoffice
and i reload (ctrl-r) i get again
mydomain.com/backoffice
If i call in UsersController in action deleteCache at the end:
return $this->redirect('/backoffice');
i get
mydomain.com/users/sbackoffice
Please note the "s" before backoffice. That seems to be a part of the config.language
If i set the Config.language to 'eng' i get
mydomain.com/users/ebackoffice
Please note now the 'e' before backoffice.
My routes.php:
Router::connect('/backoffice', array('controller' => 'users', 'action' => 'backoffice'));
Router::connect('/:language/:controller/:action/*',
array(),
array('language' => 'eng|spa')
);
Router::connect('/:language/:controller',
array('action' => 'index'),
array('language' => 'eng|spa')
);
Router::connect('/:language', array('controller' => 'technologies', 'action' => 'home'),
array('language' => 'eng|spa')
);
Any idea?
How can i debug this?

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

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

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

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.

How to set cakephp edit form custom url?

HI,
im accessing my edit_view using the url
/controller/action/group_id/id
but when i check my action it is only using
/controller/action/id
i have already tried using the following.
$params = array('url' => array('controller' => 'controller','action'=> 'action',$group_id,$id))
$this->form(model,$params)
$params = array('url' => '/controller/action/group_id/id')
$this->form(model,$params)
but its still not working.
Thanks
Don't really know what $this->form() is, but try:
echo $this->Form->create('SomeModel', array(
'url' => array(
'controller' => 'controller',
'action' => 'action',
$param_1,
$param_2
)
));

Resources