Created
January 28, 2011 06:43
-
-
Save brandoncordell/799922 to your computer and use it in GitHub Desktop.
CakePHP Link Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Link Helper | |
* | |
* @copyright Copyright 2010, Brandon Cordell | |
* @author Brandon Cordell <[email protected]> | |
* @link http://snippets.leadandrock.com/cakephp-link-helper | |
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
* | |
* Usage: | |
* | |
* Make sure you include this helper in the controller you need it (e.g. var $helpers = array('Link'); ) | |
* | |
* In your view you can call: | |
* $link->nav('Link title', array('controller' => 'pages'), 'li', null); | |
* | |
* This would output | |
* <li><a href="http://yourapp.com/pages/">link title</a></li> | |
*/ | |
class LinkHelper extends AppHelper { | |
var $helpers = array('Html'); // include the Html helper | |
var $wrap_start = ''; // default wrapping open tag | |
var $wrap_end = ''; // default wrapping close tag | |
var $current_class = 'current'; // the class you want applied to your navigations active state | |
/** | |
* Helper method to create links with some extra options typically used for | |
* navigation | |
* | |
* @param string $title The text used in between the anchor tag | |
* @param mixed $url The url for your link. Must be a valid CakePHP link, like a string (e.g. '/pages/add') or an array (e.g. array('controller' => 'pages', 'action' => 'pages', 'prefix' => 'admin')) | |
* @param array $html_options A valid CakePHP array that you pass into $html->link() | |
* @param string $wrapper The html tags you want wrapping your string (only use the tag name) | |
* @param string $wrapper_class If you want your wrapper to have specific css class | |
* @param boolean $checkForActive Check whether the current page is the same as the requested $url | |
*/ | |
function nav($title, $url, $html_options = array(), $wrapper = null, $wrapper_class = null, $checkForActive = true) { | |
$class = null; | |
// if we want to check that the given url is the current page | |
if($checkForActive) { | |
if(is_array($url)) { | |
$url_string = '/'; | |
// check to see if prefix is passed, if it is we have to manually | |
// put it up front | |
if (isset($url['prefix'])) { | |
$url_string .= $url['prefix']; | |
unset($url['prefix']); | |
} | |
if (isset($url['controller'])) { | |
$url_string .= '/' . $url['controller']; | |
unset($url['controller']); | |
} | |
if (isset($url['action'])) { | |
$url_string .= '/' . $url['action']; | |
unset($url['action']); | |
} | |
// $url_string .= '/' . implode('/', $url); | |
} else { | |
$url_string = $url; | |
} | |
// let's see if the $url string matches part of the current pages url | |
if(strpos($this->here, $url_string) !== FALSE) { | |
if ($html_options && isset($html_options['class'])) { | |
$html_options['class'] .= ' active'; | |
} else { | |
$wrapper_class = 'current'; | |
} | |
} | |
} | |
// if we chose to wrap our links in html tags (e.g. li) | |
if($wrapper) { | |
if($wrapper_class) { | |
$this->wrap_start = "<$wrapper class=\"$wrapper_class\">"; | |
} else { | |
$this->wrap_start = "<$wrapper>"; | |
} | |
$this->wrap_end = "</$wrapper>"; | |
} | |
$link = $this->Html->link($title, $url_string, $html_options); | |
$final_str = $this->wrap_start . $link . $this->wrap_end; | |
return $this->output($final_str); | |
} | |
/** | |
* Automatically create a navigation menu from your current controllers folder. Good for agile developers that use | |
* the default cake layout while they develop the application | |
*/ | |
function autoNav() { | |
if ($dh = opendir(CONTROLLERS)) { | |
$links = "<ul>\n"; | |
while (($file = readdir($dh)) !== false) { | |
// easy pattern to match cake's naming conventions | |
$regex = '/^\w+_controller\.php$/'; | |
if (preg_match($regex, $file)) { | |
// remove _controller.php from our file name | |
$len = strlen($file) - 15; | |
$file = substr($file, 0, $len); | |
// If you have a pages_controller, we'll make it the home link | |
if ($file == 'pages') { | |
$tmp = $this->Html->link('Home', '/'); | |
} else { | |
$tmp = $this->Html->link(ucfirst($file), array('controller' => $file)); | |
} | |
$links .= "<li>{$tmp}</li>\n"; | |
} | |
} | |
closedir($dh); | |
$links .= "</ul>\n"; | |
} | |
return $this->output($links); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment