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).

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

报告相同问题?

悬赏问题

  • ¥100 高价邀请复制 域天d8联网狗
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?