Laravelのすべてのセッションデータをクリアするための職人のコマンドは何ですか、私は次のようなものを探しています:
$ php artisan session:clear
しかし、明らかにそれは存在しません。コマンドラインからどのようにクリアしますか?
使ってみた
$ php artisan tinker
...
\Session::flush();
しかし、それは1人のユーザーのセッションのみをフラッシュします。すべてのユーザーのすべてのセッションをフラッシュしたいと思います。どうすればできますか?
私はこれを試しました:
artisan cache:clear
しかし、それは再びセッションをクリアしません。
このスレッドはかなり古いものです。しかし、私はファイルベースのドライバーのすべてのセッションを削除する私の実装を共有したいと思います。
$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で使用すると効果的です。
問題は、LaravelのSessionHandlerInterface
がセッションドライバーにdestroyAll()
メソッドの提供を強制しないことです。したがって、各ドライバーに対して手動で実装する必要があります。
さまざまな答えからアイデアを得て、私はこの解決策を思いつきました:
php artisan make:command FlushSessions
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();
}
}
php artisan session:flush
他のドライバーの実装は大歓迎です!