nwidart/themify

为 Laravel 应用添加基本主题功能。

dev-master 2015-02-10 19:04 UTC

This package is auto-updated.

Last update: 2024-09-08 07:02:32 UTC


README

Themify 是一个 Laravel 扩展包,以非侵入的方式提供基本主题功能。Themify 的目的是允许开发者将视图分组在主题内,每个主题都有自己的文件夹。如果你有使用 Yii 框架主题的经验,你会发现这个包的使用非常熟悉。

示例结构文件夹可能如下所示

app/
├── Http
├── ...
├── themes
│   ├── admin
│   │   ├── category
│   │   ├── dashboard
│   │   ├── ...
│   └── default
│       ├── index.blade.php
│       ├── layouts
│       ├── post
│       └── ...

Themify 预期你将主题存储在指定的文件夹中,默认为 app/themes。然后,每个主题应该在其文件夹内拥有自己的视图,就像它是一个 views 文件夹。

安装

  • 使用 composer 安装包
composer require nwidart/themify=*
  • 将 ServiceProvider 添加到你的 app/config/app.php 中的服务提供者列表
'providers' => array(
    ...
    'Illuminate\View\ViewServiceProvider',
    'Illuminate\Workbench\WorkbenchServiceProvider',

    'Nwidart\Themify\ThemifyServiceProvider',
),
  • 将 Facade 添加到你的 app/config/app.php 中的别名数组
'aliases' => array(
    ...
    'URL'             => 'Illuminate\Support\Facades\URL',
    'Validator'       => 'Illuminate\Support\Facades\Validator',
    'View'            => 'Illuminate\Support\Facades\View',

    'Themify'         => 'Nwidart\Themify\Facades\Themify',
),
  • 在应用程序中创建 themes 目录。默认情况下,Themify 预期一个 app/themes 目录,但可以在包配置中修改。

  • 使用 artisan 发布包配置

php artisan vendor:publish

然后,通过编辑 config/themify.php 来按需修改设置。

使用

首先,你必须告诉包你想要使用哪个主题。你有三种不同的方法,按照优先级排序

  1. 调用 Themify::set($theme)。其中 theme 是你想要使用的主题文件夹的名称。
  2. 在你的控制器中定义一个 public $theme 属性。
  3. 使用 Themify::defaults($theme),这是一个更改包设置中 themify::default_theme 属性的快捷方式。

一旦你定义了你的主题,你就可以使用 View 类以传统方式渲染视图。 Themify 将尝试在定义的主题文件夹中查找指定的视图。如果没有找到,它将回退到默认的 views 文件夹(或你在 app/config/view.php 中定义的任何内容)。

View::render('foo', compact($bar));

优先级

上述每种方法都有一个内部优先级分配

  • 如果使用 Themify::set($theme) 明确设置了主题,则唯一覆盖它的方法是再次使用 set()
  • 如果没有找到 set() 调用,Themify 将检查当前控制器(如果有)中的 $theme 属性。请注意,此属性应该是 public。这个检查是通过包的 ServiceProvider 为所有路由添加的简单 before 过滤器来完成的。
<?php

class MyAwesomeController extends BaseController {
    
    public $theme = 'bootstrap';

    public function index()
    {
        return View::make('index');
    }

}
  • 如果没有在控制器中找到 $theme 属性,或者当前路由没有控制器,Themify 将从其配置文件中获取值。此值可以通过使用 Themify::defaults($theme) 在运行时设置,或通过修改使用 artisan 发布的 config.php 中的 themify::default_theme 属性来设置。

主题资源

Themify 预期你在 public 目录(或你在 Laravel 配置中定义的目录)内部有一个文件夹来存储主题资源。默认情况下,此文件夹是 public/assets/themes,但可以在包配置文件中修改。

因此,这个资源文件夹应该包含每个主题的一个文件夹。例如,如果你正在使用一个 bootstrap 主题,你应该创建 public/assets/themes/bootstrap,然后在其中创建你的样式表、JavaScript 和其他资源。

辅助函数

Themify 为你的视图提供了两个方便的辅助函数: theme_url()theme_secure_url(),它们将返回当前主题资源文件夹的路径。

<link rel="stylesheet" type="text/css" href="{{ theme_url() }}/css/styles.css">
<script src="{{ theme_url() }}/js/main.min.js"></script>

示例

为多个路由设置主题

<?php

Route::filter('admin.theme', function()
{
    // We use default() so we can 
    // override later if we want
    Themify::default('admin')
});

Route::group(['prefix' => 'admin', 'before' => 'admin.theme'], function()
{
    // All of these routes will use
    // 'admin' theme

    // Override this route with a
    // different theme
    Route::get('login', function() {
        Themify::set('basic');
    });
});

为所有控制器动作设置主题

您可以在每个控制器的基础上定义您的主题,使用控制器内的 public $theme 属性

<?php

class FooController extends BaseController {

    public $theme = 'footheme';

    public function someAction()
    {
        // For this one, use a different theme
        Themify::set('bartheme');
    }

}