Url Aliasing for CakePHP | |
| Type: Full Script |
Category: Other |
| License: MIT/X Consortium License |
Language: PHP |
| Description: This allows you to pass missingController errors to a Controller that can check if there is content for this page. Use it like this: vendor('CakeUrlAlias'); CakeUrlAlias_Activate($from_url, 'pages', 'display'); | |
|
Download a raw-text version of this code by clicking on "Download Version"
<?php
/**
* You need to call this function from wihthin your config/routes.php. The first parameter *has* to be
* $from_url. The other parameters are about the controller which should handle Url Alias Pages.
*
* @param string $url
* @param string $controller
* @param string $action
* @param string $home
*/
function CakeUrlAlias_Activate(&$url, $controller = 'pages', $action = 'display', $home = 'home')
{
$parts = explode('/', $url);
if (empty($parts) || empty($parts[0]))
{
$url = $controller.'/'.$action.'/'.$home;
return;
}
$pluginOrController = array_shift($parts);
if (CakeUrlAlias_ControllerExists($pluginOrController) || CakeUrlAlias_PluginExists($pluginOrController))
return;
if (!empty($parts))
$additional = '/'.join('/', $parts);
else
$additional = null;
$url = $controller.'/'.$action.'/'.$pluginOrController.$additional;
}
/**
* Checks wether a controller with a given name exists
*
* @param string $name
* @return boolean
*/
function CakeUrlAlias_ControllerExists($name)
{
$paths = Configure::getInstance();
foreach ($paths->controllerPaths as $path)
{
if(file_exists($path.$name.'_controller.php'))
{
return true;
}
}
return false;
}
/**
* Checks wether a plugin with a given name exists
*
* @param string $name
* @return boolean
*/
function CakeUrlAlias_PluginExists($name)
{
if (is_dir(APP.'plugins'.DS.$name))
return true;
return false;
}
/**
* Checks weather a URL can be used as an Alias or if it interfers with an existing controller/plugin.
* Will return true if you can use it. False if you can't.
*
* @param unknown_type $url
* @return boolean
*/
function CakeUrlAlias_checkUrl($url)
{
$parts = explode('/', $url);
if (empty($parts) || empty($parts[0]))
return true;
$pluginOrController = array_shift($parts);
if (CakeUrlAlias_ControllerExists($pluginOrController) || CakeUrlAlias_PluginExists($pluginOrController))
return false;
return true;
}
/**
* If you experience weird behavior when changing your configuration / controller, call this function
*
*/
function CakeUrlAlias_clearCache()
{
clearstatcache();
}
?>
You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..