ENHANCEMENT: Updated SiteTree linking methods to generate nested URLs.

MINOR: Updated ContentController->Link() to force links generated by a controller to always be returned in their full form.

From: Andrew Short <andrewjshort@gmail.com>

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88476 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Andrew Short 2009-10-11 00:07:00 +00:00 committed by Sam Minnee
parent 55408946a6
commit 3599a105fd
2 changed files with 40 additions and 29 deletions

24
core/control/ContentController.php Normal file → Executable file
View File

@ -42,23 +42,17 @@ class ContentController extends Controller {
$this->failover = $this->dataRecord;
parent::__construct();
}
/**
* Return the link to this controller, but force the expanded link to be returned so that form methods and
* similar will function properly.
*
* @return string
*/
public function Link($action = null) {
return Director::baseURL() . $this->RelativeLink($action);
return $this->data()->Link(($action ? $action : true));
}
public function RelativeLink($action = null) {
if($this->URLSegment){
if($action == "index") $action = "";
// '&' in a URL is apparently naughty
$action = preg_replace('/&/', '&amp;', $action);
return $this->URLSegment . "/$action";
} else {
user_error("ContentController::RelativeLink() No URLSegment given on a '$this->class' object. Perhaps you should overload it?", E_USER_WARNING);
}
}
//----------------------------------------------------------------------------------//
// These flexible data methods remove the need for custom code to do simple stuff

45
core/model/SiteTree.php Normal file → Executable file
View File

@ -264,30 +264,47 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
}
/**
* Get the URL for this page.
* Return the link for this {@link SiteTree} object, with the {@link Director::baseURL()} included.
*
* @param string $action An action to include in the link
* @return string The URL for this page
* @param string $action
* @return string
*/
public function Link($action = null) {
if($action == "index") {
$action = "";
}
if($this->URLSegment == 'home' && !$action) return Director::baseURL();
else return Director::baseURL() . $this->URLSegment . "/$action";
return Controller::join_links(Director::baseURL(), $this->RelativeLink($action));
}
/**
* Get the absolute URL for this page by stage
* Get the absolute URL for this page, including protocol and host.
*
* @param string $action
* @return string
*/
public function AbsoluteLink() {
if($this->hasMethod('alternateAbsoluteLink')) return $this->alternateAbsoluteLink();
else return Director::absoluteURL($this->Link());
public function AbsoluteLink($action = null) {
if($this->hasMethod('alternateAbsoluteLink')) {
return $this->alternateAbsoluteLink($action);
} else {
return Director::absoluteURL($this->Link($action));
}
}
/**
* Return the link for this {@link SiteTree} object relative to the SilverStripe root.
*
* @param string $action
* @return string
*/
public function RelativeLink($action = null) {
$action = str_replace('&', '&amp;', $action);
if($this->ParentID && self::nested_urls()) {
$base = $this->Parent()->RelativeLink($this->URLSegment);
} else {
$base = $this->URLSegment;
}
return "$base/$action";
}
/**
* Returns link/current, depending on whether you're on the current page.
* This is useful for css styling of menus.