银条自定义控制器数据/变量


Silverstripe customize controller data/variables

我正在尝试将搜索结果返回到一个新的控制器,而不是从哪里执行搜索操作。问题是Results永远无法从CustomSearchResultPage.ss访问。我已经为我认为正在发生的事情添加了内联评论,我在这里的想法是对的吗?

    // Customise content with results
    $response = $this->customise(array(
        'Results' => $results ? $results->getResults() : '',
    ));
    if ($results) {
        $response = $response->customise($results);
    }
    // Use CustomSearchResultPage.ss template
    $templates = array('CustomSearchResultPage', 'Page');
    // Create a new CustomSearchResultPage page
    $page = CustomSearchResultPage::get_one('CustomSearchResultPage');
    // Build a controller using the CustomSearchResultPage
    $controller = CustomSearchResultPage_Controller::create($page);
    // Add the custom data to the newly minted controller
    $result = $controller->customise($response);
    // Return the controller and tell it how to render
    return $result->renderWith($templates);

页面似乎按预期呈现,只是变量始终为空......

恐怕你的解释有点难以理解。所以我回答我可以确定的内容如下:

  1. 执行搜索。这需要加载控制器来执行此操作。
  2. 使用结果自定义电流控制器
  3. 重新定制电流控制器本身。
  4. 设置当前(双重自定义)控制器的模板。
  5. 忽略上述所有内容。
  6. 获取随机页面(或空记录)。
  7. 为空页面创建控制器。
  8. 使用当前控制器的自定义控制器自定义新控制器。
  9. 返回该页面,不显示任何结果。

您只需在步骤 4 停止(跳过步骤 3),然后返回自定义项 ($response)。

但是,如果有某种原因您认为您需要另一个控制器(这似乎是多余的,但谁知道呢),在返回之前创建然后自定义该控制器(仅)会更好。

由于您仅使用第二个控制器来呈现结果,因此 URL 不会更改或任何内容。整个事情似乎超出了要求。

呈现此操作结果的更简单方法可能是:

return $this ->customise(['Results' => $results ? $results->getResults() : null]) ->renderWith(['CustomSearchResultPage', 'Page']);

(从我的头顶,可能需要一点改进)。