我现在正在我的项目中实现主题。我已经安装了 igaster/laravel-theme 包。
虽然我可以通过更改 config/themes.php 中的默认主题来切换主题,但我不知道如何在站点范围内更改主题- 使用如下按钮:
<a href="set_theme/2">change to 2</a>.
包的作者说我需要使用 ServiceProvide。我创造了一个。而现在……什么?
编辑:解决方案
根据 igaster 提供的答案,我让它工作 - 部分。这是我所做的更详细的描述:
在这个文件中: 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文件中注册了服务提供者:
'providers' => [
...
'App\Providers\themeSelectServiceProvider',
在我的路线文件中,我有这条路线:
路线::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 中的以下行改变了主题。
else {\Theme::set('Fawkes');}
仍然存在的问题:
在 ServiceProvider 内部,我既无法读取 Session 中存储的任何值,也无法读取任何 cookie。仅出于测试目的,我创建了
Session::put('theme', $theme);
但是从未创建过 Session 变量——不是用 Cookie,也不是用 Session。
如果可能,请提供帮助
编辑2:
我试图在 ServiceProvider 中输入以下代码:
if(Auth::check()) {
\Theme::set('Seldon');
}
但它会导致空白屏幕(我有 debug = true)。在日志文件中,我看到:
local.ERROR:带有消息“类哈希不存在”的异常“ReflectionException”
Laravel 4 对开发人员友好且令人惊叹。Laravel 5 已经翻山越岭了。我猜L6将无法使用。