mpedrera / themify
将基本主题功能添加到Laravel 4应用程序中。
Requires
- php: >=5.3.0
Requires (Dev)
- mockery/mockery: dev-master
- orchestra/testbench: 2.1.*
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2018-09-08 15:15:36 UTC
README
Themify是一个Laravel 4包,以非侵入的方式提供基本主题功能。Themify的目的是允许开发者在主题内部分组视图,每个主题都有其自己的文件夹。如果你有使用Yii框架主题的经验,你会发现这个包的使用非常熟悉。
示例结构文件夹可能如下所示
app/
├── controllers
├── config
├── ...
├── themes
│ ├── admin
│ │ ├── category
│ │ ├── dashboard
│ │ ├── ...
│ └── default
│ ├── index.blade.php
│ ├── layouts
│ ├── post
│ └── ...
Themify期望你将你的主题存储在指定的文件夹中,默认情况下是app/themes。然后,每个主题都应该在其文件夹内部有其自己的视图,就像它是一个views文件夹一样。
安装
- 使用composer安装包
"require": { "mpedrera/themify": "*" }
- 将ServiceProvider添加到
app/config/app.php中的服务提供者列表中
'providers' => array( ... 'Illuminate\View\ViewServiceProvider', 'Illuminate\Workbench\WorkbenchServiceProvider', 'Mpedrera\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' => 'Mpedrera\Themify\Facades\Themify', ),
-
在应用程序中创建
themes目录。默认情况下,Themify期望有一个app/themes目录,但可以在包配置中修改。 -
使用Artisan发布包配置:
php artisan config:publish mpedrera/themify。然后,通过编辑app/config/packages/mpedrera/themify/config/config.php按需修改设置。
使用
首先,你必须告诉包你想要使用哪个主题。你可以以三种不同的方式做到这一点,按优先级排序
- 调用
Themify::set($theme)。其中theme是你想要使用的主题文件夹的名称。 - 在你的控制器中定义一个
public $theme属性。 - 使用
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'); } }