【问题标题】:laravel layout templat lost in subfolder url bladelaravel 布局模板在子文件夹 url 刀片中丢失
【发布时间】:2023-03-30 02:34:01
【问题描述】:

当 url 是子文件夹时,我的模板布局刀片不呈现。

我做了一个测试例子来检查:

网址/测试没问题

但是

URL/tests/1/edit 丢失了外部布局模板,只呈现内容。

测试控制器:

class TestController extends AdminController {

 protected $layout = 'layouts.admin';

public function index()
{
    // load the view
    $this->layout->content=View::make('tests.index');

}
public function edit($id)
{
    //
    $course=Course::find($id);

    return View::make('tests.edit')->with(array('course'=>$course));
}

}

布局 admin.blade.php

<html><body>
{{ $content }}
</body>
</html>

测试/index.blade.php

hello

/tests 呈现源完整布局的 html 代码并在适当的站点示例上正常工作

测试/edit.blade.php

edit

/tests/1/edit 渲染没有 HTML 布局

有多种使用刀片的方法,但我认为最简单的是使用受保护的布局,但似乎有问题?

任何帮助表示赞赏。

【问题讨论】:

    标签: templates laravel blade


    【解决方案1】:

    在编辑方法中而不是

    return View::make('tests.edit')->with(array('course'=>$course));
    

    使用:

    $this->layout->content= View::make('tests.edit')->with(array('course'=>$course));
    

    【讨论】:

    • 谢谢。愚蠢的错误!
    【解决方案2】:

    在您的AdminControllerTestController 的基本控制器)中,添加布局设置,将此代码放入您的AdminController

    protected $layout = 'layouts.master';
    
    protected function setupLayout()
    {
        if ( ! is_null($this->layout))
        {
            $this->layout = View::make($this->layout);
        }
    }
    

    现在您可以使用任何带有布局的视图,使用类似这样的方式:

    $this->layout->content = View::make('tests.edit')->with(array('course'=>$course));
    

    这里,tests.edit 表示edit.blade.php(如果不是刀片也可以是edit.php)文件位于app/views/tests/ 目录中。

    在您使用的index 方法中:

    $this->layout->content=View::make('tests.index');
    

    所以layout 出现是因为您将数据设置为布局,但在其他示例中您没有将数据设置为layout,因此布局未呈现,它仅返回如下所示的视图:

    return View::make('tests.edit')->with(array('course'=>$course));
    

    因此,在基本控制器类中设置布局,这样您就不必在每个控制器中设置布局,但始终使用以下方法将数据设置到布局的 content 变量:

    $this->layout->content = 'your data';
    

    【讨论】:

      猜你喜欢
      • 2014-12-24
      • 2014-09-19
      • 2013-02-25
      • 2015-04-24
      • 2014-10-30
      • 2013-04-29
      • 2017-02-22
      • 2019-08-11
      相关资源
      最近更新 更多