You Are Here: Home » Articles » Fat Links in CakePHP

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:

There are many search engines available...

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:

  1. <?php
  2. class LinksController extends AppController
  3. {
  4.     var $argSeparator = "|";
  5.     
  6.     function index()
  7.     {
  8.         $links = explode(';', str_replace('/' . $this->params['controller'] . '/', '', $_SERVER['REQUEST_URI']));
  9.         foreach ($links as $key => $link) {               
  10.             // If link text is present...
  11.             if (preg_match('^\|^', $link)) {
  12.                 $link = explode('|', $link);
  13.                 $links[$key] = array('text' => urldecode($link[0]), 'href' => $link[1]);
  14.             // If no link text is present...
  15.             } else {
  16.                 $links[$key] = array('text' => $link, 'href' => $link);
  17.             }
  18.         }
  19.         $this->set('links', $links);
  20.     }
  21. }
  22. ?>

The view then outputs the array's values as an unordered list:

  1. <h1>My list of links</h1> <?php if (!empty($links)) { ?>
  2. <ul> <?php foreach ($links as $link) { ?>
  3.     <li><?php echo $html->link($link['text'], $link['href']); ?></li> <?php } ?>
  4. </ul> <?php } ?>

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:

  1. $Route->connect ('/links/*', array('controller'=>'links', 'action'=>'index'));

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.

http://paulherron.net/links/http://google.com;http://yahoo.com

Links with anchor text

A series of simple links with anchor text specified. A pipe symbol separated the anchor text from the link itself.
http://paulherron.net/links/Google|http://google.com;Yahoo|http://yahoo.com

Relative links

Relative links work in a similar way.
http://paulherron.net/links/Articles|/articles

Any mixture of the above

http://paulherron.net/links/http://google.com;Yahoo|http://yahoo.com;Articles|/articles

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 LinkComponent and 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 :links declaration 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 like http:// from the links.

Further reading

Leave a Comment

*
*
*

« Back to Articles

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

See More…

Back to top.