带有igaster软件包的主题 [英] Themes with package igaster
问题描述
我现在正在我的项目中实现主题. 我已经安装了igaster/laravel-theme软件包.
I am now implementing themes in my project. I have installed igaster/laravel-theme package.
虽然我可以通过更改config/themes.php中的默认主题来切换主题,但是我不知道如何更改主题 sitewide -使用如下按钮:
While I can switch themes by changing the default theme in config/themes.php, I have no idea how to change a theme sitewide - with a button like this:
<a href="set_theme/2">change to 2</a>.
该软件包的作者说我需要使用ServiceProvide. 我创建了一个. 现在...什么?
The package's author says I need to use a ServiceProvide. I created one. And now... what?
解决方案
基于igaster提供的答案,我使其得以部分运行. 这是我所做工作的更详细说明:
Based on the answer provided by igaster I made it work - PARTIALLY. This is a more detailed description of what I did:
在此文件中: App/Providers/themeSelectServiceProvider.php
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Session;
use Cookie;
use Request;
class themeSelectServiceProvider extends ServiceProvider {
public function register()
{
// for testing purpose I ignore the variable and hardcode the theme's name
// just in case I test both with and without backslash
// as the namespaces in L5 tends to be a major pain.
// neither one works.
$theme = Session::get('themeName');
// $theme = Request::cookie('themeName');
Session::put('theme', $theme);
if ($theme == 'Fawkes') {
\Theme::set('Fawkes');
}
if ($theme == 'Seldon') {
\Theme::set('Seldon');
}
else {\Theme::set('Fawkes');}
}
}
我已经在我的 config/app.php 文件中注册了服务提供商:
I have registered the service provider in my config/app.php file:
'providers' => [
...
'App\Providers\themeSelectServiceProvider',
在我的路线文件中,我有以下路线:
in my routes file i have this route:
Route :: get('set_theme/{themeName}','SitewideController @ set_theme2');
Route::get('set_theme/{themeName}', 'SitewideController@set_theme2');
在这里导致
use Response;
use Theme;
use Illuminate\Cookie\CookieJar;
class SitewideController extends Controller {
public function set_theme2($themeName)
{
\Theme::set('Seldon'); // I tested them one at a time to determine
Theme::set('Seldon'); // if there is a classname issue
if (Theme::find($themeName)) // Is $themeName valid?
{
return Redirect::to('/')->withCookie(cookie()->forever('themeName', $themeName));
// this is the only way I am able to create a cookie. Facade DOESN'T WORK!
}
Redirect::url('boooo'); // my error page
}
从现在开始,我向前迈了一步-ServiceProvider中的以下行更改了主题.
So as of now I am a step forward - the below line in my ServiceProvider changes the theme.
else {\Theme::set('Fawkes');}
问题仍然存在:
在ServiceProvider内部,我既无法读取Session中存储的任何值,也无法读取任何Cookie. 出于测试目的,我创建了
Inside the ServiceProvider I cannot read neither any value stored in Session nor any cookie. Just for testing purpose i created the
Session::put('theme', $theme);
但是从来没有创建Session变量-不是使用Cookie,也不是使用Session.
But the Session variable was NEVER created - not with Cookie, not with Session.
请尽可能提供帮助
我试图在ServiceProvider中输入以下代码:
I tried to put in the ServiceProvider the below code:
if(Auth::check()) {
\Theme::set('Seldon');
}
但是它导致黑屏(我有debug = true). 在日志文件中,我看到:
but it results in blank screen ( I have debug = true). In log file I see:
local.ERROR:消息类哈希不存在"的异常"ReflectionException"
local.ERROR: exception 'ReflectionException' with message 'Class hash does not exist'
Laravel 4对开发人员友好且令人惊叹. Laravel 5已经在山上了.我猜L6将无法使用.
Laravel 4 was developer-friendly and amazing. Laravel 5 is over the hill already. L6 will be unusable, I guess.
推荐答案
由于Laravel5中的会话是在中间件堆栈中启动的,因此您应该创建自己的中间件并将其放置在Kernel.php
中的$middleware
数组中.这是一个示例中间件:
Since Sessions in Laravel5 are initiated in the middleware stack you should create your own middleware and place it in the $middleware
array in Kernel.php
. This is a sample middleware:
class myMiddleware {
public function handle($request, Closure $next) {
$themeName = \Session::get('themeName', 'NONE');
if(\Theme::exists($themeName))
\Theme::set($themeName);
return $next($request);
}
}
您的route.php可能类似于:
your routes.php could be something like:
Route::get('setTheme/{themeName}', function($themeName){
Session::put('themeName', $themeName);
return Redirect::to('/');
});
Route::get('/', function() {
return Theme::get(); // display your views here....
});
请注意,您必须先下载最新版本(v1.0.4)才能运行
Note that you have to pull the latest version (v1.0.4) for this to work
这篇关于带有igaster软件包的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!