hexadog / laravel-themes-manager

Bring multi themes support to your Laravel application with a full-featured Themes Manager
https://laravel-themes-manager.netlify.app
MIT License
152 stars 31 forks source link

Can´t create a Child Theme #25

Closed alibaba258 closed 3 years ago

alibaba258 commented 3 years ago

Hey all,

i am not able to create a child Theme. Multiple Themes works great. But if try to create a child Theme, i get the following errors: I'm using make:theme command. Plugin Verison 1.8 Laravel 8.12

at C:\Users\dev\Desktop\Hofladen Neues System\vendor\hexadog\laravel-themes-manager\src\ThemesManager.php:324
    320▕         if (is_null($name)) {
    321▕             return null;
    322▕         }
    323▕
  ➜ 324▕         return $this->themes->first(function ($theme) use ($name, $vendor) {
    325▕             // normalize module name
    326▕             $name = str_replace(['-theme', 'theme-'], '', $name);
    327▕             // Check if $name contains vendor
    328▕             if (($pos = strpos($name, '/')) !== false) {

  1   C:\Users\dev\Desktop\Hofladen Neues System\vendor\hexadog\laravel-themes-manager\src\ThemesManager.php:106
      Hexadog\ThemesManager\ThemesManager::findByName("hls/default")

  2   C:\Users\dev\Desktop\Hofladen Neues System\vendor\hexadog\laravel-themes-manager\src\ThemesManager.php:356
      Hexadog\ThemesManager\ThemesManager::has("hls/default")
alibaba258 commented 3 years ago

Try to fix the problem by adding $this->themes = collect(); to the ThemesManager.php on __construct() Method.

Method now looks like this.

public function __construct(Factory $view, Translator $lang)
    {
        $this->view = $view;
        $this->lang = $lang;

        $this->themes = collect();

        if (Config::get('themes-manager.cache.enabled', false)) {
            $this->themes = $this->getCache();
        } else {
            $this->themes = $this->findThemes();
        }
    }

Error Message is gone, but the theme extends now the wrong parent theme.

$ php artisan theme:list
+-----------+--------+---------+-------------+-----------+---------+--------+
| Name      | Vendor | Version | Description | Extends   | Default | Active |
+-----------+--------+---------+-------------+-----------+---------+--------+
| default   | hls    | 1.0.0   |             |           | X       | Yes    |
| sauerland | hls    | 1.0.0   |             | sauerland |         | Yes    |
+-----------+--------+---------+-------------+-----------+---------+--------+
gaetan-hexadog commented 3 years ago

Hi,

weird because themes variable is normally set up by following code:

if (Config::get('themes-manager.cache.enabled', false)) {
    $this->themes = $this->getCache();
} else {
    $this->themes = $this->findThemes();
}

Have you enabled the cache ? (what's the value returned when you call dd(Config::get('themes-manager.cache.enabled', false)) ?

I can't reproduce your issue on my side.

Edit: I found the bug...it's during the initialization of themes list : $this->findThemes();. It tries to find the parent theme while themes collection is not yet populated.

I'm working on a fix for it.

alibaba258 commented 3 years ago

Hi, Cache is disable. Output of dd(Config::get('themes-manager.cache.enabled', false)) is only false

Maybe im understood something wrong but in your findThemes() method you call the following function if ($this->has($extendedThemeName))

The has() method effectively calls the findByName Method. Here you reference $this->themes which are not initialized

EDIT:

I found the bug...it's during the initialization of themes list : $this->findThemes();. It tries to find the parent theme while themes collection is not yet populated.

Yeah thats what i mean. Thanks for help.

alibaba258 commented 3 years ago

Hi,

after your fix i am able to create an child theme. But the plugin works not as i expected. This are my themes:

+-----------+--------+---------+-------------+---------+---------+--------+
| Name      | Vendor | Version | Description | Extends | Default | Active |
+-----------+--------+---------+-------------+---------+---------+--------+
| core      | hls    | 1.0.0   |             |         | X       | Yes    |
| sauerland | hls    | 1.0.0   |             | core    |         | Yes    |
+-----------+--------+---------+-------------+---------+---------+--------+

I don´t set a theme at runtime, so in my opinion the fallback theme should be used which is assigned to core. But if i render a view i get the following message View [categories.show] not found.. These view exists in the core theme but i get that error. If i create the views file in the standard laravel views folder everthing works but that gives me the idea that the fallback theme is not working.

Second problem: I set a theme at runtime (Middleware, Constructor...tried both), the plugin loads always the first theme in the table. But not the theme that i selected.

gaetan-hexadog commented 3 years ago

I'll have a look. can you please tell me were you defined your middleware and share its code please ?

gaetan-hexadog commented 3 years ago

In addition: can you confirm you set the fallback_theme in the config/themes-manager.php file and run php artisan config:cache again to update your cache ?

alibaba258 commented 3 years ago

I'll have a look. can you please tell me were you defined your middleware and share its code please ?

In my Kernel.php

protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\DomainCheckMiddleware::class,

    ];

DomainCheckMiddleware.php ist attached below. Tried Theme Name with and without vendor. Also tried to change the Theme via my Controller

 public function __construct()
    {
        ThemesManager::set('hls/sauerland');
    }

DomainCheckMiddleware

alibaba258 commented 3 years ago

In addition: can you confirm you set the fallback_theme in the config/themes-manager.php file and run php artisan config:clear again to update your cache ?

Yes can confirm that. But I have used php artisan config:clear to delete and disable the cache. 'fallback_theme' => 'core',

php artisan config:clear

dd(Config::get('themes-manager.fallback_theme')); says 'core'

gaetan-hexadog commented 3 years ago

I can't reproduce the issue. Root theme and Child are working well on my side. Even the fallback theme.

Can you please try to set a dd into the handle method of the Hexadog\ThemesManager\Http\Middleware\ThemeLoader middleware to check if you pass into the callback theme activation ?

public function handle($request, Closure $next, $theme = null)
    {
        // Do not load theme if API request
        if ($request->expectsJson()) {
            return $next($request);
        }

        if (!empty($theme)) {
            ThemesManager::set($theme);
        } else {

            if ($theme = config('themes-manager.fallback_theme')) {
                dd($theme);
                ThemesManager::set($theme);
            }
        }

        return $next($request);
    }
alibaba258 commented 3 years ago

The dd command is not executed. I get my default view. I have uploaded my Test Project. Here you can find the Repository: https://github.com/alibaba258/theme-test

Just clone and execute php artisan serve

I am expecting to see "Sauerland" but i still get "Core" from the Core Theme

gaetan-hexadog commented 3 years ago

Thanks for the repo. I get your code working and commit a fix. Now on your app I can see the sauerland theme

image

and the fallback core theme when I comment line 40 in your middleware

// $theme = 'hls/sauerland';
return parent::handle($request, $next, $theme);
image

Please let me know if its ok on your side too.

alibaba258 commented 3 years ago

You`re the man!! Works great. Thank you very much for your help :-)

gaetan-hexadog commented 3 years ago

I'm glad to hear it's working :-) Don't hesitate if you need any help