Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

2
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 5 years have passed since last update.

CakePHP3でビューにユーザ情報表示

Posted at

ログイン認証付きWebサービスを作っていると、ユーザ名を画面上に表示したいことがよくあります。
そんな時にビューにユーザ情報を表示する方法について。

isAuthorizedメソッドを使う

CakePHP3では、認証後にControllerのisAuthorizedメソッドが呼び出されるようになります。
isAuthorizedメソッドは引数にログインされているユーザ情報(ログイン処理時にsetUserしたEntity)が入ってくるので、そのEntityの必要な情報を変数に格納します。

AppController.php
public function isAuthorized($user = null){
    	//ユーザ名格納
    	$this->set('username', $user['username']);
}

ビュー側では普通にここで設定した変数名を参照すればいいだけ。

とはいえ

Controller側で変数に格納し、ビュー側で取り出すというのは何となくすっきりしない感じ。
出来ればビュー側だけで書きたいと思ったりもするけど、解決策はわかってないです。
SessionHelperとかあるみたいだから、そのあたりをうまく使えばかけるんですかね?

2
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
2
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?