1

我现在正在我的项目中实现主题。我已经安装了 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将无法使用。

4

2 回答 2

2

由于 Laravel5 中的 Session 是在中间件堆栈中启动的,因此您应该创建自己的中间件并将其$middleware放在Kernel.php. 这是一个示例中间件:

class myMiddleware {
    public function handle($request, Closure $next) {

       $themeName = \Session::get('themeName', 'NONE');

          if(\Theme::exists($themeName))
              \Theme::set($themeName);

           return $next($request);
       }

   }

您的 routes.php 可能类似于:

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)才能正常工作

于 2015-03-13T08:31:55.090 回答
0

如果您已正确遵循安装说明(https://github.com/igaster/laravel-theme),您可以像这样更改主题:

Theme::set('theme-name');

如果要通过单击链接更改主题,则必须将当前主题名称存储在某处。很可能您不想将其存储在 URI 中,因此我建议您将其存储在会话中。

于 2015-03-13T07:05:32.587 回答