Skip to content

Commit 9cc3cb0

Browse files
committed
Enforce name/title in URL for viewing docs/pkts
This helps with SEO so that pages don't appear as being "/packet/123" but instead will appear as being "/packet/123/bnls-cdkey".
1 parent 35c8253 commit 9cc3cb0

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

controllers/Document/View.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BNETDocs\Controllers\Document;
44

5+
use \BNETDocs\Controllers\Redirect as RedirectController;
56
use \BNETDocs\Libraries\Common;
67
use \BNETDocs\Libraries\Controller;
78
use \BNETDocs\Libraries\Document;
@@ -25,6 +26,26 @@ public function __construct($document_id) {
2526
}
2627

2728
public function run(Router &$router) {
29+
$model = new DocumentViewModel();
30+
$model->document_id = $this->document_id;
31+
try {
32+
$model->document = new Document($this->document_id);
33+
} catch (DocumentNotFoundException $e) {
34+
$model->document = null;
35+
}
36+
$pathArray = $router->getRequestPathArray();
37+
if ($model->document && (
38+
!isset($pathArray[3]) || empty($pathArray[3]))) {
39+
$redirect = new RedirectController(
40+
Common::relativeUrlToAbsolute(
41+
"/document/" . $model->document->getId() . "/"
42+
. Common::sanitizeForUrl(
43+
$model->document->getTitle(), true
44+
)
45+
), 302
46+
);
47+
return $redirect->run($router);
48+
}
2849
switch ($router->getRequestPathExtension()) {
2950
case "htm": case "html": case "":
3051
$view = new DocumentViewHtmlView();
@@ -35,13 +56,6 @@ public function run(Router &$router) {
3556
default:
3657
throw new UnspecifiedViewException();
3758
}
38-
$model = new DocumentViewModel();
39-
$model->document_id = $this->document_id;
40-
try {
41-
$model->document = new Document($this->document_id);
42-
} catch (DocumentNotFoundException $e) {
43-
$model->document = null;
44-
}
4559
$model->user_session = UserSession::load($router);
4660
ob_start();
4761
$view->render($model);

controllers/Packet/View.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BNETDocs\Controllers\Packet;
44

5+
use \BNETDocs\Controllers\Redirect as RedirectController;
56
use \BNETDocs\Libraries\Common;
67
use \BNETDocs\Libraries\Controller;
78
use \BNETDocs\Libraries\Exceptions\PacketNotFoundException;
@@ -26,6 +27,26 @@ public function __construct($packet_id) {
2627
}
2728

2829
public function run(Router &$router) {
30+
$model = new PacketViewModel();
31+
$model->packet_id = $this->packet_id;
32+
try {
33+
$model->packet = new Packet($this->packet_id);
34+
} catch (PacketNotFoundException $e) {
35+
$model->packet = null;
36+
}
37+
$pathArray = $router->getRequestPathArray();
38+
if ($model->packet && (
39+
!isset($pathArray[3]) || empty($pathArray[3]))) {
40+
$redirect = new RedirectController(
41+
Common::relativeUrlToAbsolute(
42+
"/packet/" . $model->packet->getId() . "/"
43+
. Common::sanitizeForUrl(
44+
$model->packet->getPacketName(), true
45+
)
46+
), 302
47+
);
48+
return $redirect->run($router);
49+
}
2950
switch ($router->getRequestPathExtension()) {
3051
case "htm": case "html": case "":
3152
$view = new PacketViewHtmlView();
@@ -36,13 +57,6 @@ public function run(Router &$router) {
3657
default:
3758
throw new UnspecifiedViewException();
3859
}
39-
$model = new PacketViewModel();
40-
$model->packet_id = $this->packet_id;
41-
try {
42-
$model->packet = new Packet($this->packet_id);
43-
} catch (PacketNotFoundException $e) {
44-
$model->packet = null;
45-
}
4660
if ($model->packet) {
4761
$model->used_by = $this->getUsedBy($model->packet);
4862
} else {

0 commit comments

Comments
 (0)