作为管理员,我可以创建页面(不要认为我必须将我的 adminpagescontroller
粘贴到这里,因为您理解其中的逻辑)。我遇到的问题是选择,但尤其是使用将用于页面的布局。
即我有三种布局:
- 带有左侧边栏的页面
- 带有右侧边栏的页面
- 全 Angular 页面(无侧边栏)
也就是说,我想创建一个销售页面左右,它使用布局“全 Angular 页面”。在我看来如何调用它?
现在我所有的 View 都以 @extends('layouts.path.file')
开头 <--- 如果您明白我的意思,我需要由数据库填充它。
最佳答案
一种方法是使用 View 编辑器来定义要使用的当前布局。 View composers 设置变量可以被你的所有 View ('*')或一些('users.profile','admin.profile')使用,所以这是一个使用用户特定布局的例子:
View::composer('*', function($view)
{
$view->with('userLayout', Auth::check() ? Auth::user()->layout : 'main');
});
在您看来,您只需要:
@extends('layouts.'.$userLayout);
如果你只需要在你的 Controller 上选择一个页面,你可以传递一个布局给它:
return View::make('myview')->with('layout', 'front.main');
并在你的 View 中使用它:
@extends('layouts.'.$layout);
如果你把它放在 table 上,你可以把它传递下去:
$layout = Pages::first()->layout;
return View::make('myview')->with('layout', $layout);
或者在你的 composer 中做同样的事情
View::composer('*', function($view)
{
$layout = Pages::first()->layout;
$view->with('layout', $layout);
});
很多人也喜欢在 Controller 中设置布局,因此您可以在 Controller 中执行以下操作:
public function showProfile()
{
$this->layout = Pages::first()->layout;
$this->layout->content = View::make('user.profile');
}
而且您的 View 不必再@extend 布局,因为您已经告诉它们要使用哪种布局。
关于php - 如何在 Laravel 中静态使用布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23063303/