Silverstripe 自定义控制器 data/variables

Silverstripe customize controller data/variables

我正在尝试 return 将搜索结果发送到新的控制器,而不是执行搜索操作的地方。问题是 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. RE-用自己自定义电流控制器
  4. 正在为当前(双定制)控制器设置模板。
  5. 忽略以上所有内容。
  6. 正在获取随机页面(或空记录)。
  7. 正在为空页面创建控制器。
  8. 用当前控制器自己定制的定制控制器来定制新控制器
  9. 正在返回该页面,但未显示任何结果。

您只需停在第 4 步(跳过第 3 步),然后return 自定义($response)。

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

因为您只使用了第二个控制器来渲染结果,所以 URL 不会有任何变化或任何变化。整个事情似乎超出了要求。

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

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

(从我的头顶,可能需要一点提炼)。