gecche / laravel-multidomain

A Laravel extension for using a laravel application on a multi domain setting
MIT License
838 stars 105 forks source link

Console Commands has no .env, and no --domain option, so they can not working if needed #114

Closed MrCanan closed 4 months ago

MrCanan commented 4 months ago

Hi,

First, good idea and work, it seems working except for one important thing =)

I have a problem, with three linked cases, perhaps it is a bug, or I missed something?

Problem - Case 1

Problem - Case 2

Problem - Case 3

Screenshot 2024-04-28 17 12 52 Screenshot 2024-04-28 17 25 48

Environnement

Model

app\Models\CustomPage.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use Vite;

class CustomPage extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
    ];
}

Migration

database\migrations\2024_04_28_171258_create_custom_pages.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('custom_pages', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            $table->string('name');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('custom_pages');
    }
};

Controller

app\Http\Controllers\CustomPageController.php

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Auth;
use Vite;
use View;
use Cache;
use Session;
use Response;

use App\Models\CustomPage;

class CustomPageController extends Controller
{
    public function showPage()
    {
        //// Work if uncomment, so to show the issue
        // Cache::flush();
        $oCustomPage = Cache::rememberForever('_oCustomPage_', function (){
            return CustomPage::first();
        });

        return view('custom_page', [
            'oCustomPage' => $oCustomPage,
        ]);
    }
}

Console Command

app\Console\Commands\RedisClearCacheTask.php

    <?php

    namespace App\Console\Commands;

    use Illuminate\Console\Command;

    use Cache;

    class RedisClearCacheTask extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'app:redis-clear-cache-task';

        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Clear Redis cache';

        /**
         * Execute the console command.
         */
        public function handle()
        {
            $bCacheFlush = Cache::flush();

            var_dump('Cache flush '.( $bCacheFlush === true ? 'done' : 'failed' ));
        }
    }

View

resources\views\custom_page.blade.php

<!doctype html>
<head>
</head>
<body>
    <h1><strong><?php echo $oCustomPage->name; ?></strong></h1>
</body>
</html>

Thank you in advance for any help, Best regards

gecche commented 4 months ago

Hi,

I think that all your problems are due to the fact that the --domain option is not available in the command shell. I think that there is some error or misstep in your configuration because I just retry my package with a super fresh installation of Laravel 11.5 and I just changed the line in the bootstrap/app.php file

Schermata 2024-04-29 alle 08 53 46

as described in the docs (without any other configuration step) and the option appears as expected.

Schermata 2024-04-29 alle 08 52 45

Let me know

Cheers

Giacomo

MrCanan commented 4 months ago

Hi,

Thank you for your quick answer,

I tested also from fresh install in 11.5.0 and it works,

So perhaps a problem in my Laravel 11.2.0 installation

In my Laravel 11.2.0, I have the line but the file is not the same,

So I update to 11.5.0 to test also

composer.json

"laravel/framework": "^11.5.0",
"gecche/laravel-multidomain": "^11.0",

I have now exactly the same bootstrap\app.php as you,

After some differents Laravel errors due to this upgrade (so it confirms my previous installation had problems), everything works

Stay as you are and continue your great project =)

Best regards