Php 不覆盖核心CMS路由的自定义路由
我想在我的站点中实现一些自定义路由,最终将作为数据库查找来定义返回的内容。我一直在看哪个没有按预期工作(主要是因为它是针对SS2的) 我有我的主要路线。yml:Php 不覆盖核心CMS路由的自定义路由,php,routing,silverstripe,Php,Routing,Silverstripe,我想在我的站点中实现一些自定义路由,最终将作为数据库查找来定义返回的内容。我一直在看哪个没有按预期工作(主要是因为它是针对SS2的) 我有我的主要路线。yml: --- Name: mysiteroutes After: - 'framework/routes#coreroutes' --- Director: rules: 'create//$Action': 'CreateController' 'profile//$Person/$Action/': 'Profil
---
Name: mysiteroutes
After:
- 'framework/routes#coreroutes'
---
Director:
rules:
'create//$Action': 'CreateController'
'profile//$Person/$Action/': 'ProfileController'
My_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'));
}
}
}
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"));
}
}
class BaseController extends ModelAsController {
public function getNestedController() {
$params = $this->getRequest()->params();
if (/*check for $params['URLSegment'] here */) {
//return custom controller
} else {
return parent::getNestedController();
}
}
}
class PersonController extends Controller {
private static $allowed_actions = array(
);
public function index(SS_HTTPRequest $request) {
return $this->renderWith(array("PersonHomePage", "Page"));
}
}
我的配置文件控制器:
class BaseController extends ModelAsController {
public function getNestedController() {
if ($this->getRequest()->params('URLSegment') =='Test') {
return new ProfileController();
} else {
return parent::getNestedController($this->getRequest()->params('URLSegment'));
}
}
}
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"));
}
}
class BaseController extends ModelAsController {
public function getNestedController() {
$params = $this->getRequest()->params();
if (/*check for $params['URLSegment'] here */) {
//return custom controller
} else {
return parent::getNestedController();
}
}
}
class PersonController extends Controller {
private static $allowed_actions = array(
);
public function index(SS_HTTPRequest $request) {
return $this->renderWith(array("PersonHomePage", "Page"));
}
}
这在没有动态路由的情况下可以正常工作,但是我想捕获(例如)/example/
下的任何内容检查是否有db条目(通过我尚未编写的自定义脚本),如果没有通过cms路由(即检查是否有预定义的路由,然后在返回404之前检查是否有页面)。我怎样才能做到这一点
我尝试了
“$URLSEMENT//$Action/$Detail/$Option”:“ProfileController”
,我可以正确路由和加载自定义模板等,但这意味着/admin/
,/create/
和其他预定义路由将无法工作。这里是一种方法,尽管可能有更好的方法:
---
Name: mysiteroutes
---
Director:
rules:
'create//$Action': 'CreateController'
'profile//$Person/$Action': 'ProfileController'
---
Name: modelascontrollerroutes
After: '#rootroutes'
---
Director:
rules:
'': 'HomePage_Controller'
'$URLSegment//$ID': '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'));
}
}
}
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"));
}
}
class BaseController extends ModelAsController {
public function getNestedController() {
$params = $this->getRequest()->params();
if (/*check for $params['URLSegment'] here */) {
//return custom controller
} else {
return parent::getNestedController();
}
}
}
class PersonController extends Controller {
private static $allowed_actions = array(
);
public function index(SS_HTTPRequest $request) {
return $this->renderWith(array("PersonHomePage", "Page"));
}
}
自定义控制器示例:
class BaseController extends ModelAsController {
public function getNestedController() {
if ($this->getRequest()->params('URLSegment') =='Test') {
return new ProfileController();
} else {
return parent::getNestedController($this->getRequest()->params('URLSegment'));
}
}
}
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"));
}
}
class BaseController extends ModelAsController {
public function getNestedController() {
$params = $this->getRequest()->params();
if (/*check for $params['URLSegment'] here */) {
//return custom controller
} else {
return parent::getNestedController();
}
}
}
class PersonController extends Controller {
private static $allowed_actions = array(
);
public function index(SS_HTTPRequest $request) {
return $this->renderWith(array("PersonHomePage", "Page"));
}
}
下面是一种方法,尽管可能有更好的方法:
---
Name: mysiteroutes
---
Director:
rules:
'create//$Action': 'CreateController'
'profile//$Person/$Action': 'ProfileController'
---
Name: modelascontrollerroutes
After: '#rootroutes'
---
Director:
rules:
'': 'HomePage_Controller'
'$URLSegment//$ID': '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'));
}
}
}
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"));
}
}
class BaseController extends ModelAsController {
public function getNestedController() {
$params = $this->getRequest()->params();
if (/*check for $params['URLSegment'] here */) {
//return custom controller
} else {
return parent::getNestedController();
}
}
}
class PersonController extends Controller {
private static $allowed_actions = array(
);
public function index(SS_HTTPRequest $request) {
return $this->renderWith(array("PersonHomePage", "Page"));
}
}
自定义控制器示例:
class BaseController extends ModelAsController {
public function getNestedController() {
if ($this->getRequest()->params('URLSegment') =='Test') {
return new ProfileController();
} else {
return parent::getNestedController($this->getRequest()->params('URLSegment'));
}
}
}
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"));
}
}
class BaseController extends ModelAsController {
public function getNestedController() {
$params = $this->getRequest()->params();
if (/*check for $params['URLSegment'] here */) {
//return custom controller
} else {
return parent::getNestedController();
}
}
}
class PersonController extends Controller {
private static $allowed_actions = array(
);
public function index(SS_HTTPRequest $request) {
return $this->renderWith(array("PersonHomePage", "Page"));
}
}
也许你想要神奇的方法?更多的是与路由有关,而不是覆盖核心功能,不确定神奇的方法如何帮助纠正这个问题?你想要动态路由。您可以将url段、uri部分或完整请求传递到_get()方法中,以便为任何(pre/post/partial)路由进行自定义调用。在这里,你可以有单独的配置。据我所知,这可以用于调用函数,但是我的配置仍然会覆盖SilverStripe设置的预定义配置,这更是我面临的问题。可能相关:也许你想要神奇的方法?更多的是路由,而不是覆盖核心功能,不确定神奇的方法将如何帮助纠正这个问题?你想要动态路由。您可以将url段、uri部分或完整请求传递到_get()方法中,以便为任何(pre/post/partial)路由进行自定义调用。在这里,您可以使用单独的配置。据我所知,这可以用于调用函数,但是我的配置仍然会覆盖SilverStripe设置的预定义配置,这更是我面临的问题。可能相关: