Aurora078 Posted September 29, 2010 Share Posted September 29, 2010 Ok, so i did mod_rewrite. Anyway, i can't figure out how im going to make this function for it.. Basically its a function ill use for all my links i output, so if i ever want to change the rewrite, i can just edit a function and they all fix. Anyone have something similar or can help? Quote Link to comment Share on other sites More sharing options...
Zeggy Posted September 29, 2010 Share Posted September 29, 2010 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. Quote Link to comment Share on other sites More sharing options...
Aurora078 Posted October 5, 2010 Author Share Posted October 5, 2010 Thank you(:. It works fine. I had to make a few adjustments for my structure, but you did most of the things i couldnt think of to do. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.