1

这是我之前的问题的一个分支。到目前为止,我的问题是以未散列的形式获取 Cookie 变量 - 请参见下面的第 6 点。

我无法设置主题。至于现在,我已经通过反复试验确定在我的 ServiceProvider 内部我不能使用 cookie 或会话变量。

我的 ServiceProvider 的内容如下所示:

App/Providers/themeSelectServiceProvider.php

<?php namespace App\Providers;


use Illuminate\Support\ServiceProvider;
use Session;
use Cookie;
use Request;
use Auth;

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);
    Session::put('check', 'What the...');

    if ($theme == 'Fawkes') {

        \Theme::set('Fawkes');
    }
    if ($theme == 'Seldon') {

        \Theme::set('Seldon');
    }

    else {\Theme::set('Fawkes');}







}

}

我已经在我的config/app.php文件中注册了服务提供者:

'providers' => [
...
    'App\Providers\themeSelectServiceProvider',

情况是:

  1. 我可以在我应用程序的任何位置的任何视图中打印 Session::get('themeName') 。

  2. 我也可以打印 Cookie - 在视图中可以看到

  3. 线

    否则 {\Theme::set('Fawkes');}

更改主题 - 但我需要手动编辑值(主题名称)以在它们之间切换。if 条件中的命令永远不会起作用。

  1. 当我尝试使用此行设置会话值时

    会话::put('theme', $theme);

什么都没发生。但是

Session::put('check', 'What the...');

在 ServiceProvider 中声明后,可以在视图中看到。

  1. 当我使用命名空间声明时,我得到了错误。这意味着解析了 ServiceProvider 内容。

请帮我解决问题!

编辑:

正如@igaster 所说,Session 对我不起作用。伤心。

尽管如此,igaster/themes readme.md 中提到的 Cookie 解决方案也不起作用。我做了这个测试:

  1. 在任何 VIEW 文件中,此代码

    请求::cookie('themeName');

产生 cookie 的预期值(“Seldon”)

当我在 ServiceProvider 中执行以下代码时

$theme = Request::cookie('themeName');
Session::put('theme', $theme);

然后在视图中打印会话变量,我得到这样的东西:

eyJpdiI6IjU0eUF6Y3YwaGdmSEhaTVplS3hyQ1E9PSI4InZhbHVl5oiMWhQY2hYRjZ6YzBmUjRDSjc5amNXUT09IiwibWFjIjoiMTEyYTENTc0MjM2ZmE5YzA5OWYwYWE5MjE3OTNhYjZkMTU5NmVmZDcwY

似乎 Cookie 没有经过哈希处理。

谁能告诉我该怎么做才能获得编码值?

  1. 此外,在 ServiceProvider 中,我无法从数据库中获取主题值 通常对于已登录的用户,我会这样做:

        $ic = Usersetting::where('user_id',Auth::id())->first();
        $theme = $ic->InterfaceComplexity;
        \Theme::set($ic->InterfaceComplexity);
    

但在这里我得到空白屏幕。当我使用这样的条件代码时也是如此:

    if(Auth::check()) {
        \Theme::set('Seldon');
    }

local.ERROR:异常 'Symfony\Component\Debug\Exception\FatalErrorException' 带有消息 'Uncaught exception 'ErrorException' 和消息 'Undefined property: Symfony\Component\Debug\ExceptionHandler::$charset' in D:\www!NiePozwalam\供应商\symfony\debug\Symfony\Component\Debug\ExceptionHandler.php:199

4

2 回答 2

0

而是将代码移动到boot()方法中。这将在所有服务提供者都注册后运行:

class ThemeServiceProvider {

    public function boot()
    {
        $theme = Session::get('theme');
    }

    public function register()
    {
        // Register any application services here
    }

}
于 2015-03-14T11:14:05.697 回答
0

会话在服务提供者内部不起作用,因为它们稍后在中间件堆栈中启动......

您应该改用中间件

于 2015-03-14T10:06:52.303 回答