【问题标题】:Silverstripe customize controller data/variablesSilverstripe 自定义控制器数据/变量
【发布时间】:2016-02-27 19:16:57
【问题描述】:

我正在尝试将搜索结果返回到新控制器,而不是执行搜索操作的位置。问题是Results 永远无法从CustomSearchResultPage.ss 访问。我已经为我认为正在发生的事情添加了内联 cmets,我的想法是否正确?

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

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

【问题讨论】:

    标签: php silverstripe


    【解决方案1】:

    恐怕你的解释有点难以理解。因此,我正在回答我可以确定的内容:

    1. 执行搜索。这需要加载一个控制器来执行此操作。
    2. 使用结果自定义当前控制器
    3. RE - 自行自定义当前控制器。
    4. 为当前(双重定制)控制器设置模板。
    5. 无视以上所有。
    6. 获取随机页面(或空记录)。
    7. 为空页面创建一个控制器。
    8. 用自己自定义的当前控制器的自定义控制器自定义新控制器。
    9. 返回该页面,但未显示任何结果。

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

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

    由于您只使用了第二个控制器来呈现结果,因此 URL 不会发生任何变化或任何事情。整个事情似乎超出了要求。

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

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

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

    【讨论】:

    • 如果我错了,请纠正我,但是使用上述方法,搜索结果将始终返回到Page_Controller 我最终想要的是在CustomSearchResultPage_Controller 上返回的结果而不是
    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多