简体   繁体   English

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

[英]Laravel define default layout from controller in good way

I googled two hours, but not found answer. 我用Google搜索了两个小时,但未找到答案。 Maybe you can help. 也许你可以帮忙。

When I define in MyController : 当我在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);
        }
    }
}

In entries\\index.blade.php : entries \\ index.blade.php中

@section('content')
    <h1>Test</h1>
@endsection

And in layouts\\default.blade.php : layouts \\ default.blade.php中

<!DOCTYPE html>
<html>
<body>
    @yield('content')
</body>
</html>

Nothing is displaying. 什么都没有显示出来。 And I don't understand why. 我不明白为什么。 When I am replacing in MyController return part with: 当我在MyController返回部分替换时:

$this->layout->nest('content', 'entries.index', array(
    'entries' => $entries
));

Then all is working, but.. It looks not clean and I don't like it. 然后一切正常,但是..它看起来不干净,我不喜欢它。 When adding in every view @layout('layouts.default') all is working good too, but it is not DRY. 在每个视图中添加@layout('layouts.default')一切都运行良好,但它不是DRY。 For example, in RoR I don't need to do such things in Controller. 例如,在RoR中,我不需要在Controller中执行此类操作。

How can define in MyController one layout and use return View::make (I think that this is right way) or how can do it better? 如何在MyController中定义一个布局并使用return View::make (我认为这是正确的方法)或者如何做得更好?

To use layouts in controllers, you must specify: 要在控制器中使用布局,您必须指定:

public $layout = 'layouts.default';

You can also not return in the method as it will override the use of $layout. 您也不能在方法中返回,因为它将覆盖$ layout的使用。 Instead, to embed your content within the layout you use: 相反,要将您的内容嵌入您使用的布局中:

$this->layout->nest('content', 'entries.index', array('entries' => $entries));

No need to return anything in your method now. 现在无需在方法中返回任何内容。 This will fix it. 这将解决它。


Edit: 编辑:

"Beautiful Ways?" “美丽的方式?”

$this->layout->nest('content', 'entries.index')->with('entries', $entries);


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


$this->layout->entries = $entries;
$this->layout->nest('content', 'entries.index');

It should be 它应该是

public $layout = 'layouts.default';

Here's the link Templating - The Basics 这是链接模板 - 基础知识

Now you can return your layout like this 现在您可以像这样返回您的布局

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

/**
 * Setup the layout used by the controller.
 *
 * @return void
 */

/*Set a layout properties here, so you can globally
  call it in all of your Controllers*/
protected $layout = 'layouts.default';

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

} }

class HomeController extends BaseController { class 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');
}

} }

Ref: http://teknosains.com/i/tutorial-dynamic-layout-in-laravel-4 参考: http//teknosains.com/i/tutorial-dynamic-layout-in-laravel-4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM