Laravel 4 Controller Templating / Blade - Correct method? [closed]

人盡茶涼 提交于 2019-12-02 18:08:37

I don't store any layout information in the controller, I store it in the view via

@extends('layouts.master')

When I need to return a view in the controller I use:

return \View::make('examples.foo')->with('foo', $bar);

I prefer this approach as the view determines what layout to use and not the controller - which is subject to re-factoring.

I don't like either of them. Layouts are probably the weirdest part of Laravel. The controller version doesn't really make sense; all methods of the controller then require that view. The @yield version is a mess of boilerplate. I made up this "method specific layouts":

public function index()
{
    return View::make('layouts.main', [
        'layout_data' => 'sup'
    ])->nest('content', 'welcome', [
        'view_data' => 'sup'
    ]);
}

I think it should be mentioned in the docs that this is an option.

I prefere the second one, because it shows a more clear separation between your view and controller code. It seems more logical to me that title would be a property of a content view instead of combining your welcome view with your welcome title each time.

In the end both are correct and will work, but the second alternative is more maintainable.

I prefer the 1st method since some sites have a dynamically generated title from the database. It's easy to pass the title in using the first method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!