代码之家  ›  专栏  ›  技术社区  ›  thedarklord47

使用布局/视图时,在Laravel 4.2中设置响应标题

  •  0
  • thedarklord47  · 技术社区  · 7 年前

    我在我的控制器中以以下方式使用布局,如文档中所述(请注意,没有返回任何内容):

    public function someControllerFunction () {
    
        $this->layout = View::make('layout');
        $this->layout->content = View::make('page', $params);
    
    }
    

    cache-control 标题如下:

    <?php
        header('Cache-Control: public');
        header('Cache-Control: max-age=600');
        Log::info("headers set in layout");
    ?>
    

    <?php
        header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        Log::info("headers set in View");
    ?>
    

    我试着把它放在我的视线里,但没有雪茄。查看视图中的php正在执行的日志 在布局中,导致布局定义的标题覆盖视图定义的标题。

    return Response::view('page')->header('Cache-Control', '...');
    

    但是我如何使用布局做出响应呢?我在文件中找不到任何提及此事的地方。

    1 回复  |  直到 7 年前
        1
  •  0
  •   thedarklord47    7 年前

    找到了答案。您可以制作布局并将其传递给响应(将标题附加到响应),然后按如下方式返回:

    $this->layout = View::make('layout');
    $this->layout->content = View::make('page', $params);
    
    $response = Response::make($this->layout);
    $response->header(...);
    
    return $response;