Skip to content

Commit

Permalink
Merge pull request #4 from helpfulrobot/convert-to-psr-2
Browse files Browse the repository at this point in the history
Converted to PSR-2
  • Loading branch information
axllent committed Dec 21, 2015
2 parents e31298b + edf8dc4 commit 1719a3a
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions code/TrailingSlashExt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@
* All other getVars() are preserved.
*/

class TrailingSlashExt extends Extension {

public function onBeforeInit() {
$this->redirectIfNecessary();
}

public function redirectIfNecessary() {

$request = $this->owner->request;

if ($request && $request->isGET()) {
$url = $request->getVar('url');
$urlPathInfo = pathinfo($url);

/* ignore Ajax requests and URLs that contain an extension */
if (Director::is_ajax() || isset($urlPathInfo['extension'])) {
return false;
}

/* remove all trailing slashes if more than 1 */
$url = preg_replace('/\/\/+$/', '', $url);
if (strlen($url) > 1 && substr($url, -1, 1) != '/') {
$params = $request->getVars();
unset($params['url']);
$redirect = Controller::join_links(
$url, '/', ($params) ? '?' . http_build_query($params) : null
);
return $this->owner->redirect($redirect, 301);
}
}

}

class TrailingSlashExt extends Extension
{

public function onBeforeInit()
{
$this->redirectIfNecessary();
}

public function redirectIfNecessary()
{
$request = $this->owner->request;

if ($request && $request->isGET()) {
$url = $request->getVar('url');
$urlPathInfo = pathinfo($url);

/* ignore Ajax requests and URLs that contain an extension */
if (Director::is_ajax() || isset($urlPathInfo['extension'])) {
return false;
}

/* remove all trailing slashes if more than 1 */
$url = preg_replace('/\/\/+$/', '', $url);
if (strlen($url) > 1 && substr($url, -1, 1) != '/') {
$params = $request->getVars();
unset($params['url']);
$redirect = Controller::join_links(
$url, '/', ($params) ? '?' . http_build_query($params) : null
);
return $this->owner->redirect($redirect, 301);
}
}
}
}

0 comments on commit 1719a3a

Please sign in to comment.