-->

Themes with package igaster

2019-06-02 10:13发布

问题:

I am now implementing themes in my project. I have installed igaster/laravel-theme package.

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>.

The package's author says I need to use a ServiceProvide. I created one. And now... what?

Edit: solution

Based on the answer provided by igaster I made it work - PARTIALLY. This is a more detailed description of what I did:

In this file: 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');}







}

}

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');

which leads here:

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

}

So as of now I am a step forward - the below line in my ServiceProvider changes the theme.

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

The problem which persists:

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);

But the Session variable was NEVER created - not with Cookie, not with Session.

Please help if possible

Edit 2:

I tried to put in the ServiceProvider the below code:

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

but it results in blank screen ( I have debug = true). In log file I see:

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

Laravel 4 was developer-friendly and amazing. Laravel 5 is over the hill already. L6 will be unusable, I guess.

回答1:

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);
       }

   }

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....
});

Note that you have to pull the latest version (v1.0.4) for this to work



回答2:

If you have properly followed the installation instructions (https://github.com/igaster/laravel-theme), you can change the theme like this:

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

If you want to change the theme by clicking a link, you have to store current theme name somewhere. Most probably you don't want to store it in URI, so I advice you to store it in a session.