【问题标题】:Laravel define default layout from controller in good wayLaravel 很好地从控制器定义默认布局
【发布时间】:2012-11-10 05:10:10
【问题描述】:

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

当我在 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);
        }
    }
}

entries\index.blade.php

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

layouts\default.blade.php中:

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

什么都没有显示。我不明白为什么。当我在 MyController 中替换时,返回部分:

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

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

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

【问题讨论】:

    标签: php laravel


    【解决方案1】:

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

    public $layout = 'layouts.default';
    

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

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

    现在无需在您的方法中返回任何内容。这将解决它。


    编辑:

    “美丽的方式?”

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

    【讨论】:

    • 是的 - 正如我在问题帖子中所写的那样,它正在工作,但恕我直言,它很难看。有没有更清晰/更简单的方法?
    • @Orbitum 到底什么是丑陋的?
    • 每个动作都做$this-&gt;layout-&gt;nest('section', 'viewfile')很奇怪。所以我想知道 - 是否有更简洁的方式来呈现带有/不带有部分和变量的视图或没有。如果没有更短的方法 - 那么我不会搜索更多。
    • 请记住,您可以链接 with() 和 nest() 方法。 $this-&gt;layout-&gt;with('title', 'Hello World')-&gt;nest('content', 'entries.index', compact('entries'))
    • $this->layout->nest('content', 'entries.index')->with('entries', $entries);不工作..条目未定义
    【解决方案2】:

    应该是

    public $layout = 'layouts.default';
    

    这是链接Templating - The Basics

    现在你可以像这样返回你的布局

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

    【讨论】:

      【解决方案3】:
       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);
          }
      }
      

      }

      类 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');
      }
      

      }

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

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 2013-02-23
      相关资源
      最近更新 更多