2013-06-05 72 views
0

之前得到我使用Laravel 4Laravel 4內容佈局

的新鮮構建今天我有一個dashboardController

class DashboardController extends BaseController { 

protected $layout = 'layouts.dashboard'; 

public function index() 
    { 
     $this->layout->content = View::make('dashboard.default'); 
    } 

} 

我有一個簡單的路線

Route::get('/', '[email protected]'); 

我有一個視圖中的刀片佈局/ layouts/dashboard.blade.php 爲了保存所有實際HTML中的每個人生病,請使用模擬。

<html> 
<head> 
    <title></title> 
</head> 
<body> 
@yield('content') 
</body> 
</html> 

我在視圖/儀表板/默認葉片文件具有以下(編輯爲簡單起見)

@section('content') 
<p>This is not rocket science</p> 
@stop 

出於某種原因,內容獲取佈局之前產生。

+0

你的BaseController是否做了特別的事情?確保它有'setupLayout()'方法:https://github.com/laravel/laravel/blob/master/app/controllers/BaseController.php - 否則,你可能不需要發佈更多的dashboard.default文件。 –

+0

是的,檢查'BaseController'上的'setupLayout()'。我只是建立了一個1:1的環境,其中包含你在這裏發佈的內容並且它可以工作(至少我想它是這樣的 - 你沒有提供預期的結果和實際結果)。你也可以嘗試'$ this-> layout-> nest('content','dashboard.default')'而不是'$ this-> layout-> content' ... – jolt

回答

0

我發現這樣做比較容易。我會創造我的主刀片的文件,像這樣

<html> 
    <body> 
     @yield('content'); 
    </body> 
</html 

而在這我想用頂部的主葉片的文件,我會把

@extends('master') 

然後內容,像這樣

@section('content') 
// content 
@stop 

希望這有助於。

0

當您使用控制器佈局時,即$this->layout->...,則可以作爲變量訪問數據,而不是部分。因此,要訪問您的版面內容,你應該使用...

<html> 
<head> 
    <title></title> 
</head> 
<body> 
    <?php echo $content; ?> 
</body> 
</html> 

而在你的部分,你不會用@section@stop ...

<p>This is not rocket science</p> 
4

我使用了不同的方法來設置全局佈局使用自定義過濾器進行路由。將下面的過濾器到應用程序/ filters.php

Route::filter('theme', function($route, $request, $response, $layout='layouts.default') 
{ 
    // Redirects have no content and errors should handle their own layout. 
    if ($response->getStatusCode() > 300) return; 

    //get original view object 
    $view = $response->getOriginalContent(); 

    //we will render the view nested to the layout 
    $content = View::make($layout)->nest('_content',$view->getName(), $view->getData())->render(); 

    $response->setContent($content); 
}); 

和現在,而不是在控制器類設置佈局屬性,可以組的路由,應用過濾器,如下圖所示。

Route::group(array('after' => 'theme:layouts.dashboard'), function() 
{ 

    Route::get('/admin', '[email protected]'); 

    Route::get('/admin/dashboard', function(){ return View::make('dashboard.default'); }); 

}); 

當創建的意見,確保在所有子視圖使用@section(「sectionName」),並使用@yield(「sectionName」)在佈局意見。

+0

這似乎是一個非常先進的概念,你有沒有在互聯網的荒野中找到它? *也許有更先進的東西在那裏* – jolt

+0

嗨@jolt,我想我從Laravel論壇採取了這個想法,但是這是Laravel 3特有的,稍有不同。 – Raftalks