web-dev-qa-db-ja.com

Laravel

Laravelのすべてのセッションデータをクリアするための職人のコマンドは何ですか、私は次のようなものを探しています:

$ php artisan session:clear

しかし、明らかにそれは存在しません。コマンドラインからどのようにクリアしますか?

使ってみた

$ php artisan tinker  
...
\Session::flush();

しかし、それは1人のユーザーのセッションのみをフラッシュします。すべてのユーザーのすべてのセッションをフラッシュしたいと思います。どうすればできますか?

私はこれを試しました:

artisan cache:clear

しかし、それは再びセッションをクリアしません。

9

このスレッドはかなり古いものです。しかし、私はファイルベースのドライバーのすべてのセッションを削除する私の実装を共有したいと思います。

        $directory = 'storage/framework/sessions';
        $ignoreFiles = ['.gitignore', '.', '..'];
        $files = scandir($directory);

        foreach ($files as $file) {
            if(!in_array($file,$ignoreFiles)) unlink($directory . '/' . $file);
        }

Linuxコマンド「rm」を使用しなかったのはなぜですか?

PHPはLaravelの前提条件の1つであり、Linuxはそうではありません。このLinuxコマンドを使用すると、プロジェクトはLinux環境でのみ実装可能になります。それが理由です。 PHP Laravelで使用すると効果的です。

0
Hamees A. Khan

問題は、LaravelのSessionHandlerInterfaceがセッションドライバーにdestroyAll()メソッドの提供を強制しないことです。したがって、各ドライバーに対して手動で実装する必要があります。

さまざまな答えからアイデアを得て、私はこの解決策を思いつきました:

  1. 作成コマンド
php artisan make:command FlushSessions 
  1. app/Console/Commands/FlushSessions.phpにクラスを作成
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class FlushSessions extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'session:flush';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Flush all user sessions';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $driver = config('session.driver');
        $method_name = 'clean' . ucfirst($driver);
        if ( method_exists($this, $method_name) ) {
            try {
                $this->$method_name();
                $this->info('Session data cleaned.');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        } else {
            $this->error("Sorry, I don't know how to clean the sessions of the driver '{$driver}'.");
        }
    }

    protected function cleanFile () {
        $directory = config('session.files');
        $ignoreFiles = ['.gitignore', '.', '..'];

        $files = scandir($directory);

        foreach ( $files as $file ) {
            if( !in_array($file,$ignoreFiles) ) {
                unlink($directory . '/' . $file);
            }
        }
    }

    protected function cleanDatabase () {
        $table = config('session.table');
        DB::table($table)->truncate();
    }
}
  1. コマンドを実行
php artisan session:flush

他のドライバーの実装は大歓迎です!

0
jotaelesalinas