drjtua5953 2016-02-27 19:16
浏览 49
已采纳

Silverstripe自定义控制器数据/变量

I'm trying to return search results to a new controller than where the search action was performed from. Problem is Results is never accessible from CustomSearchResultPage.ss. I've added inline comments for what I think is happening, am I right in my thinking here?

    // 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);

The page seems to render as expected just the variable is always empty...

  • 写回答

1条回答 默认 最新

  • douzhechi2435 2016-02-29 21:18
    关注

    Your explanation is a little hard to follow I'm afraid. So I'm answering for what I can ascertain as below:

    1. Performing a search. This requires loading a controller to do as such.
    2. Customising the current controller with the results
    3. RE-customising the current controller with itself.
    4. Setting the template for the current (double customised) controller.
    5. Disregarding all of the above.
    6. Fetching a random page (or an empty record).
    7. Creating a controller for the empty page.
    8. Customising the new controller with the customised controller of the current controller customised with itself.
    9. Returning that page, which shows no results.

    You need only stop at step 4 (skip step 3), and return the customisation ($response).

    If there is some reason you think you need another controller however (which seems superflous, but who knows), creating and then customising that one (only) before returning it would be better.

    Being that you have only used this second controller for rendering a result, the URL will not have changed or anything. The whole thing seems beyond requirements.

    A much more simple way to render a result from this action would probably be:

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

    (from the top of my head, may need a little refining).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改