Jump to content
MakeWebGames

Link Builder function?


Aurora078

Recommended Posts

Maybe something like this?

 

<?php
class LinkBuilder
{
   private $links;

   public function __construct($links=0)
   {
       if ($links !== 0)
           $this->links = $links;
       else
           $this->links = array();
   }

   public function addLink($name, $page)
   {
       if (array_key_exists($name, $this->links))
           return false;

       $this->links[$name] = $page;
       return true;
   }

   public function getLink()
   {
       $args = func_get_args();

       $name = $args[0];
       if (!array_key_exists($name, $this->links))
           return '';

       $args = array_slice($args, 1);
       //Build your link here
       //Example:
       $link = '/' . $this->links[$name] . '/';
       foreach ($args as $arg)
       {
           //Append arguments
           $link .= $arg . ';';
       }

       //Calling getLink('home', $arg1, $arg2, $arg3)
       //Will generate a link like:
       // /home/arg1;arg2;arg3;

       //Just edit this function if you change your url structure

       return $link;
   }
}


//How to setup the class
//Create a links array inside a 'urls' file and include it in each page
$links = array();
$links['home'] = 'home';
$links['attack'] = 'attack';
$links['attack-result'] = 'attack/result';
$links['forum-home'] = 'forum';
$links['forum-new-topic'] = 'forum/new';

$linkBuilder = new LinkBuilder($links);



//How to use it
echo '[url="', $linkBuilder->getlink('home'), '"]Home[/url]
';
//Link to /home/

$username = 'Test';
echo '[url="', $linkBuilder->getLink('attack', $username), '"]Attack ', $username, '[/url]
';
//Link to /attack/Test;

$forum_id = 5;
$category_id = 12;
echo '[url="', $linkBuilder->getLink('forum-new-topic', $forum_id, $category_id), '"]Create new topic[/url]
';
// Link to /forum/new/5;12;

 

But you shouldn't mess around with your urls much after you've written your site...

This class is more useful in being able to generate consistent urls on every page.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...