# in a CakePHP $this->Html->link - php

I was wondering how I go about adding a "#" to a cakephp url without it being escaped. Ive tried 'escape' => false but to no success. I am doing this to try and link to a id="comments" section of the page that has contains the comments. Here is my code for the linking:
<?php echo $this->Html->link($post['Post']['total_comments'].' comments', array('controller' => 'posts', 'action' => 'view', $post['Post']['slug'].'#comments'),array('class' => 'comments-icon')); ?>
Any help would be appreciated.

Try this:
echo $this->Html->link($post['Post']['total_comments'].' comments', array(
'controller' => 'posts',
'action' => 'view',
$post['Post']['slug'],
'#' => 'comments' //Or #comments only without key
), array('class' => 'comments-icon'));

Related

CakePHP 3 - How to make routes for one resource with multiple parameters

My Url like this:
/api/v2/data/:id/:id/:id/:id
Here, i have created plugin named ApiV2 and 'data' is resource. I am trying to create routes below :
$routes->resources('Data', [
'map' => [
':id' => [
'action' => 'index',
'method' => 'GET'
]
]
]);
But its showing Error: Missing Method in DataController.
I can handle this url by modifying it like :
/api/v2/data/term1/:id/term2/:id/term3/:id/term4/:id
But i don't want to use that type. What changes should i need to do in routes?. Any help will be grateful. Thanks
You can try this one:
<?php
// Routing code
Router::connect('/news/:slug/',
array(
'controller' => 'news',
'action' => 'view'
),
array(
'slug' => '[a-zA-Z0-9_-]+'
)
);
?>
<?php
// HTML Link code.
echo $this->Html->link(
$news['News']['title'],
array(
'controller' => 'news',
'action' => 'view',
'slug' => $news['News']['slug']
)
);
?>
If it is not working for you please let me know :)
Thanks

Add 2 tags on link in cakephp

I am new in cakephp and i have to create a link with 2 tags like following html
<li><i class="fa fa-th-list"></i> <span>Shop</span></li>
And in cake php i did something like this:
<li><?php echo $this->Html->link(
// $this->Html->tag('i', array('class' => array('fa', 'fa-th-list'))),
$this->Html->tag('span', 'Video / Imagini', null),
array(
'controller' => 'users',
'action' => 'video',
),
array('escape' => FALSE)) ?></li>
How can i add the tag?
I search to internet and on cake book but no details about the second tag.
Thank you for your time.
Your code was almost correct. I made only some little changes:
<li><?php
echo $this->Html->link(
$this->Html->tag('i', '', array('class' => array('fa', 'fa-th-list'))) .
$this->Html->tag('span', 'Video / Imagini'),
array(
'controller' => 'users',
'action' => 'video',
),
array('escape' => false)
);
?></li>

cakePHP route element directs to missing controller action?

I'm trying to setup the following routing in cakePHP 2.3:
domain/news/slug
I've followed the cookbook guidelines on routing and the route that gets created is correct. The problem I run into is that when selecting the link I get the 'Missing Method in NewsController' error message.
Here's what I've configured:
Router::connect(
'/news/:slug/',
array('controller' => 'news', 'action' => 'view'),
array(
'pass' => array('slug'),
'slug' => '[^_]+'
)
);
I'm passing in the slug with a regular expression (any string that does not include an underscore).
This is my link in the index page:
<?php echo $this->Html->link(
$news['News']['title'],
array(
'controller' => 'news',
'action' => 'view',
'slug' => $news['News']['slug']
)
); ?>
As mentioned, the URL is built correctly, and looks like this: /news/test-slug-news-story
But when I click on it I get the 'Missing Method in NewsController' error message
Is it obvious what I'm missing, cause I've looked at this too long to be able to see it.
Thanks, Paul
You can try this one:
<?php
// Routing code
Router::connect('/news/:slug/',
array(
'controller' => 'news',
'action' => 'view'
),
array(
'slug' => '[a-zA-Z0-9_-]+'
)
);
?>
<?php
// HTML Link code.
echo $this->Html->link(
$news['News']['title'],
array(
'controller' => 'news',
'action' => 'view',
'slug' => $news['News']['slug']
)
);
?>
If it is not working for you please let me know :)
Thanks
As mentioned above, I discovered that by having a backslash after 'slug' in the route setting, the controller interprets the ':slug/' as the controller action.
One of those 'doh' moments.
Code should look like this:
Router::connect(
'/news/:slug',
array('controller' => 'news', 'action' => 'view'),
array(
'pass' => array('slug'),
'slug' => '[a-zA-Z0-9_-]+'
)
);

cakePHP formHelper postLink image

I want to output an image with a hyperlink wrapped around it instead of just a text hyperlink using cakePHP's formHelper::postLink function.
Does anyone know how to do this? I tried multiple things but couldn't get it working.
<?php echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $country['Country']['id']),
array('confirm' => __('Are you sure you want to delete ').$country['Country']['name'].'?')
)?>
So instead of 'Delete' I want to display an image.
Here's what works for me.
echo $this->Form->postLink(
$this->Html->image('icn_trash.png', array('alt' => __('Effacer'))), //le image
array('action' => 'delete', $artist['Artist']['id']), //le url
array('escape' => false), //le escape
__('Êtes-vous sûr de vouloir effacer artiste #%s?', $artist['Artist']['id']) //le confirm
); //le voila
Try this :
echo $this->Form->postLink(
$this->Html->image('delete.png',
array("alt" => __('Delete'), "title" => __('Delete'))),
array('action' => 'delete', $items['Item']['id']),
array('escape' => false, 'confirm' => __('Are you sure?'))
);
If I understand your question correctly I dont think you want to use $this->Form->postLink
I think this page is exactly what you're after: http://book.cakephp.org/view/1441/image
This uses $this->Html->image to create the image and then you can pass a URL through as one of the parameters to specify the surrounding anchor link.
You can wrap in image inside a link element, but you need to set the escape option to false, like this:
echo $this->Html->link(
$this->Html->image('your_image_here.jpg', array(
'alt' => 'Alternative Text for your image',
'title' => 'Optional tooltip text for your image'
),
array(
'controller' => 'YourController',
'action' => 'someAction'
),
array(
'escape' => false // Add this to avoid Cake from printing the img HTML code instead of the actual image
)
);
That should do the trick.
echo $this->Html->link(
$this->Html->image("recipes/6.jpg", array("alt" => "Brownies")),
array(
'controller' => 'recipes',
'action' => 'view',
'id' => 6,
'comments' => false
)
)
function delete_image(){
if ($this->Session->read('Auth.User.id')) {
$this->User->id = $this->Session->read('Auth.User.id');
$this->User->updateAll(
array('User.image' => "''"),
array('User.id' => $this->User->id)
);
$this->Session->setFlash('The image has been deleted.');
$this->redirect(array('action' => 'profile'));
}
}

CakePHP Routing help

I have the following link: <?php echo $this->Html->link($post['Portfolio']['title'],
array('controller' => 'portfolio', 'action' => 'view', Tiny::toTiny($post['Portfolio']['id']), Inflector::slug($post['Portfolio']['title'])),
array('title' => $post['Portfolio']['title'])); ?>
which creates urls like http://driz.co.uk/portfolio/view/3z/Paperview_Magazine
BUT I want to remove the view part of the url via the routing system. So far I have implemented this:
Router::connect('/portfolio/id:/slug:', array('controller' => 'portfolio', 'action' => 'view', 'id', 'slug'));
But it doesn't work. Can anyone help?
You should try this:
Router::connect('/portfolio/*', array('controller' => 'portfolio', 'action' => 'view'));

Categories