desmart/laravel-layout

此包已被放弃,不再维护。未建议替代包。
此包最新版本(1.2.0)的许可证信息不可用。

适用于 Laravel4 的简单页面结构控制器

1.2.0 2014-10-23 08:48 UTC

This package is auto-updated.

Last update: 2022-02-01 12:24:17 UTC


README

Build Status

安装

desmart\laravel-layout 添加到 composer.json 的要求中

{
  "require": {
    "desmart/laravel-layout": "1.2.*"
  }
}

使用 composer update 更新您的包或使用 composer install 安装。

app/config/app.php 中添加

  • 'DeSmart\Layout\LayoutServiceProvider', 到 providers
  • 'Layout' => 'DeSmart\Layout\Facades\Layout', 到别名。

概述

此包提供了 DeSmart\Layout\Controller 类,它类似于常规页面控制器。
但是,它可以用来描述完整的页面结构。

为此,只需定义 $layout,它是一个基本页面模板,以及 $structure,它是一个包含部分块定义的数组。

部分块是一系列回调(让我们称它们为 actions),将在模板中指定的位置运行。

示例页面模板

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div class="container">
      <div class="main">{{ $main }}</div>
      <div class="right">{{ $right }}</div>
    </div>
  </body>
</html>

示例控制器

<?php
class SampleController extends \DeSmart\Layout\Controller {

  protected $layout = 'layouts.default';
  
  protected $structure = array(
    'main' => array(
      'FancyBanner@show',
      'TopStories@show',
    ),
    'right' => array(
      'Menu@showTopProducts',
    ),
  );
  
  public function showProducts() {
    $this->structure['main'] = array(
      'Products@showAll',
    );
    
    return $this->execute();
  }
  
  public function showOne() {
    $this->changeLayout('layouts.product');
    $this->structure['main'] = array(
      'Products@showOne',
    );
    
    return $this->execute();
  }

}

示例路由

<?php
Route::get('/', 'SampleController@execute');
Route::get('/products', 'SampleController@showProducts');
Route::get('/products/{product_id}', 'SampleController@showOne');

动作

每个动作都是一个回调字符串,将在 DeSmart\Layout\Controller@execute 调用期间调用。
每个动作都可以获取在路由中定义的参数,只需将它们定义为函数参数(public function showOne($product_id) {})。

布局外观

此包提供了带有 dispatch 方法的 Layout 外观。它可以直接在模板中执行操作。

<header>{{ Layout::dispatch('HomeController@head') }}</header>

dispatch() 可以接受带有命名回调参数的数组参数

class FancyController {

  public function test($name, $title = 'sir. ') {}
  
}

<header> {{ Layout::dispatch('FancyController@test', array('name' => 'Hans')) }} </header>

注意,它还处理默认参数。

警告

此包按原样提供。目前它只是一个概念,整个想法可能会改变。

只需将其视为早期的 alpha 版本。