2015-03-13 29 views
1

我現在在我的項目中實現主題。 我已經安裝了igaster/laravel-theme包。主題與包igaster

雖然我可以通過在配置/ themes.php更改默認主題切換主題,我不知道如何改變一個主題站點範圍 - 有這樣一個按鈕:

<a href="set_theme/2">change to 2</a>. 

包的作者說我需要使用ServiceProvide。 我創建了一個。 現在...什麼?

編輯:解

基於由igaster提供的答案我把它工作的 - 部分。 這是什麼,我做了更詳細的描述:

在這個文件中: 應用/供應商/ 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');} 







} 

} 

我已經註冊了服務提供商在我配置/應用程序。 PHP文件:

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

在我的路線文件,我有這條航線:

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以下行更改主題。

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

其中仍然存在的問題:

中的ServiceProvider我不能讀既不存儲在會話也沒有任何cookie的任何值內。 只是用於測試目的,我創建了

Session::put('theme', $theme); 

但Session變量從未創造 - 不是餅乾,不與會話。

請幫助如果可能的話

編輯2:

我試圖在的ServiceProvider把下面的代碼:

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

,但它會導致黑屏(我有調試=真) 。 在日誌文件中我看到:

local.ERROR: exception 'ReflectionException' with message 'Class hash does not exist'

Laravel 4是開發人員友好和驚人的。 Laravel 5已經在山頂上了。我想,L6將無法使用。

回答

2

由於Laravel5中的會話是在中間件堆棧中啓動的,因此您應該創建自己的中間件並將其放置在Kernel.php$middleware陣列中。這是一個示例中間件:

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)的工作

+0

我向前邁進了一步。請檢查我編輯的問題。 看來我無法訪問ServiceProvider中的Session或Cookie。 感謝任何提示。 Thx – Peter 2015-03-13 10:56:01

+0

安裝1.0.4後得到這個錯誤:local.ERROR:在D:\ www \!NiePozwalam \ vendor \ igaster \ laravel-theme \ src \ Themes中沒有找到消息'Theme'Fawkes'的異常'Exception'。 php:50 我恢復到1.0.3,問題消失了。 – Peter 2015-03-14 13:08:03

+0

你應該在'themes.php'配置文件中加入'Fawkes'.... – igaster 2015-03-14 16:19:47

0

如果你已經正確按照安裝說明操作(https://github.com/igaster/laravel-theme),你可以改變這樣的主題:

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

如果你想通過單擊鏈接來更改主題,您可以選擇存儲當前主題名稱某處。很可能你不想將它存儲在URI中,所以我建議你將它存儲在一個會話中。

+0

我已經在這個問題上花了3個小時。看起來,L5對我來說是一個b ...。 請查看我的編輯 - 我把我的問題歸結爲別的。也許你可以找到ServiceProvider問題的解決方案。 – Peter 2015-03-13 10:57:38