Thursday 15 January 2015

php - Should I use laravel Auth::user everytime I reference or define once on page -



php - Should I use laravel Auth::user everytime I reference or define once on page -

using laravel 4 , auth feature.

let want check if logged in id matches of blog creator id.

if (auth::user()->id == $blog->blogownerid) { // }

that works fine, need check in various places in page.

the other alternative set user id , blog creator id variables easier use.

// @ start of page $blogownerid = $blog->blogownerid; if (auth::check()) { $userid = auth::user()->id; }

now im not sure if there performance gains not calling functions everytime, unless when referencing variable check anyway.

just looking right way of doing things :)

craig.

yes, there (small) performance benefit not calling function on , on again. minor comes downwards more readable , prefer.

i this:

$userid = auth::check() ? auth::user()->id : false; $blogownerid = $blog->blogownerid; // checks if ($blogownerid === $userid) { // }

make sure using === don't false positives if $blog->blogownerid 0

php laravel laravel-4

No comments:

Post a Comment