MainelyDesign.com Blog

Adding Anchors to Your CakePHP Generated Urls (#link)

Posted on 07/02/2009 at 09:02 am by Kevin Wentworth
Viewed 35,301 times | 2 comments

This may seem simple and trivial, but it took me a little while to figure out.  If it saves someone a little trial-and-error then great (I know I won't forget how to do it).

I needed to create a redirect in CakePHP that redirected to an inline, on-page anchor- <a name="destination">Scroll to Here</a>.  Typing the link directly into the address bar of the browser, it would need to look like this: http://www.mainelydesign.com/blog/view/1219/#destination.  The #destination is what I needed to figure out how to append to the redirected url.  Turns out it was easy...

Use the Pound Sign (#) as a Key in Your Link Options

  1. echo $html->link('Click Me, Please', array('controller'=>'blog', 'action'=>'view', 1219, '#' => 'destination'));

or, Use the Pound Sign (#) as a Key in Your Redirect Call

  1. $this->redirect(array('controller'=>'blog', 'action'=>'view', 1219, '#'=>'destination'));

That was easy wasn't it?  Note the '#'=>'destination'.  Sometimes it's the easy things that can stop and make you think, though.

Cheers,

-Kevin Wentworth

Bookmark and Share

Tags for Adding Anchors to Your CakePHP Generated Urls (#link)

Cakephp | Web Programming | Usage | Usability

Comments for this Posting

Posted by SooxnogYBix

on 5/3/10

Wassup, First time here, this site seems like a very informative board.
Will try to help in to the best I am able :)

Posted by bancer

on 29/4/10

The same result you can achieve with

echo $html->link('Click Me, Please', array('controller'=>'blog', 'action'=>'view', 1219, '#destination'));

http://sourceforge.net/projects/bancer/

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design