1
我想在我的站點中實現一些自定義路由,最終將作爲數據庫查找來定義返回的內容。我一直在尋找this example這是不是按預期工作(主要是因爲它是爲SS2)。不覆蓋核心CMS路由的自定義路由
我有我的主要routes.yml:
---
Name: mysiteroutes
After:
- 'framework/routes#coreroutes'
---
Director:
rules:
'create//$Action': 'CreateController'
'profile//$Person/$Action/': 'ProfileController'
我_config.php:
Director::addRules(2, array(
'$URLSegment//$Action/$Detail/$Option' => 'BaseController',
));
我BaseController:
class BaseController extends ModelAsController {
public function getNestedController() {
if ($this->getRequest()->params('URLSegment') =='Test') {
return new ProfileController();
} else {
return parent::getNestedController($this->getRequest()->params('URLSegment'));
}
}
}
我ProfileController可:
class ProfileController extends Controller {
private static $allowed_actions = array(
'test'
);
public function index(SS_HTTPRequest $request) {
return $this->renderWith(array("ProfileHomePage", "Page"));
}
public function test(SS_HTTPRequest $request) {
print_r($request->params());
return $this->renderWith(array("ProfileHomePage", "Page"));
}
}
這工作正常,但沒有動態路由,但我想捕捉任何(例如)/example/
檢查是否有一個數據庫條目(通過自定義腳本,我還沒有寫),如果不路由它cms(即檢查是否有預定義的路由,然後在返回404之前是否有頁面)。我怎樣才能做到這一點?
我確實嘗試了'$URLSegment//$Action/$Detail/$Option': 'ProfileController'
,我可以正確地路由並加載自定義模板等,但這意味着/admin/
,/create/
和其他預定義的路由不起作用。
也許你想__magic方法? – Deep
它更多的是與路由而不是重寫核心功能,不知道__magic方法如何幫助解決這個問題? – Rudiger
你想要動態路線。您可以將url段或uri部分或完整請求傳遞給__get()方法,以便爲您的任何(前/後/部分)路由進行自定義調用。在這裏你可以有單獨的配置。 – Deep