Fat Links in CakePHP
Using one hyperlink to point to multiple pages
Tagged with CakePHP and Web Development
Posted on 20/1/08 by Paul Herron
Occasionally, several links are relevant to the same piece of text. The usual approach to presenting this is to apply separate anchors to individual words within that text:
This of course makes very little sense semantically, and it might not be immediately obvious that more than one link is available.
Another option is to make a ul or an ol and place that inline within the document. That can be detrimental to the flow of the text though, and isn't really in the non-linear spirit of hypertext.
The solution below takes a fat link approach, whereby several URLs are rolled into one. An appropriate script can then extract the links and present them as a list on their own page.
There are many search engines available...
This code is written for CakePHP 1.2, and has a controller action processing the fat link before sending an array of links to the view:
- <?php
- class LinksController extends AppController
- {
- var $argSeparator = "|";
- function index()
- {
- $links = explode(';', str_replace('/' . $this->params['controller'] . '/', '', $_SERVER['REQUEST_URI']));
- foreach ($links as $key => $link) {
- // If link text is present...
- // If no link text is present...
- } else {
- }
- }
- $this->set('links', $links);
- }
- }
- ?>
The view then outputs the array's values as an unordered list:
Finally, an optional extra is to declare a route to remove the need for '/index' to be included in the URL. This will appear in app/config/routes.php like so:
Usage
Simple links
A series of simple links. No anchor text is defined, so the href is presented instead. Links are separated with a semi-colon.
Links with anchor text
A series of simple links with anchor text specified. A pipe symbol separated the anchor text from the link itself.Relative links
Relative links work in a similar way.Any mixture of the above
Considerations
- Be warned that spammers love to get their links on websites, and this method provides an open opportunity to do just that. Think carefully about where to implement it, and keep an eye on your analytics for signs of abuse.
- The syntax chosen to separate the links is important. In the above example,
|and;are used as separators. These are arbitrary; any glyphs can be used so long as they don't appear in any of the URLs you're trying to list. - For simplicity, I've presented the bulk of this code in the controller. It might be tidier to make something like a
LinkComponentand simply call that from the controller. - There might be a more Cakeish way to achieve this. For example, an alternative to
$_SERVER['REQUEST_URI']could be a:linksdeclaration in a custom route. This would grab the links string and pass it to the controller, but some tweaking would be required to stop Cake stripping out things likehttp://from the links.
Further reading
- Reviving Advanced Hypertext, an article by Jakob Nielsen
- Fat Link article at Wikipedia
Leave a Comment
Article Tags
Show all articles, or just those tagged as:
Apache (1)
CakePHP (5)
Domains (1)
Ethics and That (1)
Freeware (1)
Open Source (1)
Servage (1)
SMS (1)
Software (1)
WAMP (1)
Web Development (6)
Windows (2)
Feed
The articles RSS feed is available here.
Elsewhom
-
Stuntbox.
David Sleight's Blog -
mattheaton.com.
Bluehost Blog
Matt is CEO of Bluehost, a successful web hosting provider -
hackdiary.
Matt Biddulph's blog -
graphpaper.
Art Science Business
