1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[EC-CUBE4]serviceファイル内でユーザのログイン状態を判別する方法

Posted at

環境

EC-CUBE 4.0.5
symfony 3.4.42
php 7.3

実行方法


// appを取得
$app = \Eccube\Application::getInstance();
// user情報を取得
$user = $app['user'];
// ゲストユーザは"anon."が格納される
if ($user != "anon.") {
 
  // 会員限定で行いたい処理

}

これでログイン状態のユーザ(会員)を判別して処理を行えるうようになります。

参考に

isGranted('ROLE_USER')は使えない

こちらはAbstractControllerを継承しているController内でしか使えないです。
twig内で使うようなis_granted('ROLE_USER')も使えません。

他の実行例

こちらの記事にある
$this->app->user()を使って取れる場合もあるようですが、私の環境では取れませんでした。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?