2013-08-12 42 views
0

我想問如何在Laravel中爲同一個控制器定義多個佈局。 這裏的情況如下所示:如何在同一個控制器內使用多個佈局?

我有一個控制器主頁,我在這個控制器中有兩個動作,一個叫做步驟,另一個叫登錄。

我希望他們都加載不​​同的佈局。

,我用來做這個是如下的方式:

protected $layout = "layouts.page"; 

public function index() 
{ 
    // Get to the page of the website making steps 
    $this->layout->content = View::make('steps'); 
} 

我可以定義多個佈局?也許傳遞一個數組如下:

protected $layout = array('first' => "layouts.page", 'second' => 'layouts.second'); 

回答

2

使用View Composers或看路過子視圖意見http://laravel.com/docs/responses#views下的部分。

您還可以指定爲在http://laravel.com/docs/templates#blade-templating定義佈局多個部分

編輯:

如果要定義來自同一個控制器不同的看法總綱,然後定義佈局在查看它自我。看看Using A Blade Layout

@extends用於定義視圖本身的佈局。

希望這有助於你在找什麼。

+0

我想爲同一控制器定義多個佈局實現.. – osos

+0

你能在你的複式佈局的意思詳細闡明,從上面的問題,它看起來像你想要多個主佈局。是對的嗎? –

+0

是的, 我想多個主佈局 – osos

3

最好的解決方案是創建生成您的視圖的方法,你的嵌套佈局倍數:

return View::make('layouts.master', array()) 
     ->nest('section_one', YOUR_SECOND_MASTER, array()) 
     ->nest... 

和停止設置protected $layout與佈局。

0

這不是很常見的做法,我還沒有測試過,但值得一試。

在你的控制器的方法:

$this->layout = View::make('layouts.master1"); 
1

如果你看一下BaseController,你的控制器可能延伸,你會看到佈局變量最終僅用作日的任何舊查看電子的結果。

換句話說,您的$layout變量只是一個視圖。你可以在你的控制器創建任何$layout變量:

<?php 

class MyController extends BaseController { 

    protected $layout; 

    protected $layout_alt; 

    // Here we're over-riding setupLayout() from 
    // the BaseController 
    protected function setupLayout() 
    { 
     if (! is_null($this->layout)) 
     { 
      $this->layout = View::make($this->layout); 
     } 

     if (! is_null($this->layout_alt)) 
     { 
      $this->layout_alt = View::make($this->layout_alt); 
     } 

    } 

} 

然後在您的視圖,可以返回:

$this->layout_alt->content = View::make('steps'); 

當然,可能性是無限的Abishek [R Srikaanth指出。你也可以用Blade來做一些奇特的事情:D

1

我這樣做的方式和@ fideloper的答案很相似。

protected $layout; 
private $_layout = null; 

public function __construct() 
{ 

} 

private function _setupLayout() 
{ 
    if (! is_null($this->_layout)) 
    { 
     $this->layout = View::make($this->_layout); 
    } 
} 

public function home() { 
    $this->_layout = 'layouts.1col_public'; 
    $this->_setUpLayout(); 
    $this->layout->content = View::make('static/home'); 
} 

public function about() { 
    $this->_layout = 'layouts.2col_public'; 
    $this->_setUpLayout(); 
    $this->layout->active_menu = 'about'; 
    $this->layout->content = View::make('static/default'); 
} 
3

我這樣

$this->layout = View::make('layout.master'); 
$this->layout->content = View::make('step.demo') 
相關問題