在我当前的Contrete5项目中,我有一个页面,其中包含一个url参数。所以url看起来是这样的:
domain.com/[singlepagename]/[id]
控制器具有相应的查看功能:
function view($id) { ... }
现在我需要访问onstart()函数中的id。但是CCD_ 1抛出错误并且CCD_。
知道我如何在on_start()
中获得url部分吗?
我以前遇到过这种情况。。。问题是C5的请求对象直到各种包控制器的启动事件被激发之后才被初始化。我相信人们想出的唯一解决方案是在包控制器的on_start函数中手动初始化Request类。如果您查看dispatcher.php文件,您将看到第129行(在Concrete56.0.1中)周围的以下代码块:
// figure out where we need to go
$req = Request::get();
if ($req->getRequestCollectionPath() != '') {
if (ENABLE_LEGACY_CONTROLLER_URLS) {
$c = Page::getByPath($req->getRequestCollectionPath(), 'ACTIVE');
} else {
$c = $req->getRequestedPage();
}
} else {
$c = Page::getByID($req->getRequestCollectionID(), 'ACTIVE');
}
$req = Request::get();
$req->setCurrentPage($c);
if ($c->isError()) {
// if we've gotten an error getting information about this particular collection
// than we load up the Content class, and get prepared to fire away
switch($c->getError()) {
case COLLECTION_NOT_FOUND:
$v = View::getInstance();
$v->render('/page_not_found');
break;
}
}
所以我认为你可以将所有这些复制到包控制器的on_start函数中,然后你就有了$req
对象来获取你的路径信息和变量。
注意:我从Concrete56.0.1中复制了该代码。如果您使用的是不同版本的系统,您不应该只使用我在上面粘贴的内容,而是自己从/concrete/dispatcher.php
文件中复制适当的代码