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.