Laravel 很好地定义了控制器的默认布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13501922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:25:47  来源:igfitidea点击:

Laravel define default layout from controller in good way

phplaravel

提问by Orbitum

I googled two hours, but not found answer. 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:

条目\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 MyControllerreturn 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. For example, in RoR I don't need to do such things in Controller.

然后一切正常,但是..它看起来不干净,我不喜欢它。在每个视图中添加时,一切@layout('layouts.default')都运行良好,但不是 DRY。例如,在 RoR 中我不需要在 Controller 中做这样的事情。

How can define in MyControllerone layout and use return View::make(I think that this is right way) or how can do it better?

如何在MyController一个布局中定义和使用return View::make(我认为这是正确的方法)或者如何做得更好?

回答by Joel Larson

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. Instead, to embed your content within the layout you use:

您也不能在方法中返回,因为它会覆盖 $layout 的使用。相反,要将您的内容嵌入您使用的布局中:

$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');

回答by afarazit

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();

回答by ngakak

 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 {

类 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