Php Laravel以良好的方式从控制器定义默认布局

Php Laravel以良好的方式从控制器定义默认布局,php,laravel,Php,Laravel,我在谷歌上搜索了两个小时,但没有找到答案。也许你能帮忙 当我在MyController中定义时: class MyController extends Base_Controller { public $layout = 'layouts.default'; public function get_index() { $entries = Entry::all(); return View::make('entries.index')

我在谷歌上搜索了两个小时,但没有找到答案。也许你能帮忙

当我在MyController中定义时:

class MyController extends Base_Controller {
    public $layout = 'layouts.default';

    public function get_index() {
        $entries = Entry::all();
        return View::make('entries.index')
            ->with('entries', $entries);
        }
    }
}
@section('content')
    <h1>Test</h1>
@endsection
<!DOCTYPE html>
<html>
<body>
    @yield('content')
</body>
</html>
条目\index.blade.php中:

class MyController extends Base_Controller {
    public $layout = 'layouts.default';

    public function get_index() {
        $entries = Entry::all();
        return View::make('entries.index')
            ->with('entries', $entries);
        }
    }
}
@section('content')
    <h1>Test</h1>
@endsection
<!DOCTYPE html>
<html>
<body>
    @yield('content')
</body>
</html>
那么一切都正常了,但是。。它看起来不干净,我不喜欢它。当在每个视图中添加
@layout('layouts.default')
时,所有视图都工作正常,但并不干燥。例如,在RoR中,我不需要在Controller中执行这些操作

如何在MyController中定义一个布局并使用返回视图::make(我认为这是正确的方法),或者如何做得更好?

应该是这样的

public $layout = 'layouts.default';
这是链接

现在可以像这样返回布局

$view = View::make('entries.index')->with('entries', $entries);
$this->layout->content = $view->render();

要在控制器中使用布局,必须指定:

public $layout = 'layouts.default';
您也不能在方法中返回,因为它将覆盖$layout的使用。相反,要将内容嵌入到您使用的布局中,请执行以下操作:

$this->layout->nest('content', 'entries.index', array('entries' => $entries));
现在不需要在方法中返回任何内容。这会解决它的


编辑:

“美丽的方式?”

}

类HomeController扩展了BaseController{

public function showHome()
{   
    /*now you can control your Layout it here */
     $this->layout->title= "Hi I am a title"; //add a dynamic title 
     $this->layout->content = View::make('home');
}
}

参考:

是的-它就像我在问题帖中写的那样工作,但我觉得它很难看。没有更清晰/更简单的方法?@Orbitum到底什么是丑陋的?对每个操作执行
$this->layout->nest('section','viewfile')
是很奇怪的。所以我想知道-有没有更简洁的方法来呈现带有/不带节和变量的视图,或者没有。如果没有更短的方法,那么我就不会搜索更多。记住,可以链接with()和nest()方法<代码>$this->layout->with('title','Hello World')->nest('content','entries.index',compact('entries'))$this->layout->nest('content','entries.index')->with('entries',$entries');不工作。。条目未定义虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,则仅链接的答案可能无效。