lamotivo / themes
本包的最新版本(v1.2.0)没有提供许可证信息。
Laravel主题管理器
v1.2.0
2022-02-26 08:16 UTC
Requires
- php: >=7.0
- illuminate/support: ^5.4 | ^6.2 | ^7.0 | ^8.0 | ^9.0
- lamotivo/assets: ^1.3
README
简介
一个用于管理应用程序中主题的Laravel包。
安装
composer require lamotivo/themes
然后,发布配置
php artisan vendor:publish --provider="Lamotivo\Themes\ThemeServiceProvider" --tag=config
主题结构
默认主题位置
resources/themes
您可以在 config/themes.php
中自由设置自己的位置。
名为 red 的主题的目录结构
resources/themes/red/views
resources/themes/red/assets/js
resources/themes/red/assets/css
resources/themes/red/assets/public
来自主题的视图比 resources/views/
优先。
它们也覆盖了命名空间视图。
使用主题
您可以使用以下方法激活一个主题
- 在运行时
<?php
use Theme;
Theme::activate('red');
- 使用中间件
<?php
// app\Http\Kernel.php
protected $middlewareGroups = [
'web' => [
// ...
\Lamotivo\Themes\Middleware\UseTheme::class,
],
// ...
];
protected $routeMiddleware = [
// ...
'theme' => \Lamotivo\Themes\Middleware\UseTheme::class,
];
- 在 .env 文件中
ACTIVE_THEME=red
AUTO_ACTIVATE_THEME=true
然后您可以在路由中使用它
Route::group([
'prefix' => '/red-theme',
'middleware' => 'theme:red',
], function() {
// ...
});
访问主题资源
主题公共资源默认位于 public/vendor/themes
目录。您可以在 config/themes.php
中覆盖 vendor/themes
前缀。
要访问资源,您应该为您的主题创建符号链接。
使用Artisan命令实现这一点
php artisan themes:link
这将创建从 public/vendor/themes/*
到 resources/themes/*/assets/public
的主题链接。
在您的Blade视图中,您可以使用 @theme_url
助手
<img src="@theme_url('some-image.jpg')">
上面的示例将生成如下代码
<img src="/vendor/themes/red/some-image.jpg">
有时您可能想从固定的主题中获取一些资源,如下所示
<img src="@theme_url('some-image.jpg', 'winter')">
上面的示例将生成如下代码
<img src="/vendor/themes/winter/some-image.jpg">
如果活动主题扩展自另一个主题,资源也会继承。
处理JS和CSS/SCSS资源
所有JS和CSS资源默认位于主题位置相对的 assets/js
和 assets/css
路径。
您可以根据自己的喜好使用任何其他结构。
我们使用 lamotivo/assets 来处理资源。在您的Blade视图中,您可以使用如下代码
@asset_add('js/some-script.js')
@asset_add('js/another-script.js')
@asset_add('css/some-styles.scss')
<!DOCTYPE html>
<html>
<head>
<title></title>
@asset_css
</head>
<body>
<!-- ... -->
@asset_js
</body>
</html>
如果活动主题扩展自另一个主题,JS和CSS资源也会继承。