ctaljaardt's avatar

Where is the best place to put this code

Okay,

So i have a header which includes the users image, so i have the image uploaded to amazon s3 and thats all fine, but when i want to echo the image i want to know where the best place to put the code is.

So here is the logic to finding out if there is an image available thats used in my show profile controller

    public function showProfile($username, Filesystem $filesystem)
    {
        $user = User::where('username', e($username))->first();
        if ($user != null) {
        //Might be slow in loading pages
            $jpg = $filesystem->exists('/profile/avatar/' . $user->id .'.jpg');
            $png = $filesystem->exists('/profile/avatar/' . $user->id .'.jpg');
            if($jpg != false){
                return view('profile.userprofile', array('user' => $user,
                   'image' => 'http://s3.amazonaws.com/profile/avatar/' . $user->id .'.jpg'));
            } elseif ($png != false){
                return view('profile.userprofile', array('user' => $user,
                   'image' => 'http://s3.amazonaws.com/profile/avatar/' . $user->id .'.png'));
            } elseif ($png == false && $jpg == false) {
                return view('profile.userprofile', array('user' => $user,
                   'image' => 'http://s3.amazonaws.com/profile/avatar/default-avatar.png'));
            }
        }
    }

So it looks if a jpg or png exists with the users id number and it it does, it returns an $image variable with the url of the image and then in the view we can do a simple

 {!!HTML::image($image, 'profile_pic', array('class' => 'img-responsive'))!!}

which will display the image.

The problem comes when using a template that imported

So i have this to call my template

@extends('../layout/header')

And inside there there is a little bit where it has the users photo.

So if i wanted to get the users image that isnt on their profile page where would i put that code ?

The header doesnt come from any controllers and i would have to copy and paste the code a lot.

I dont know if my question is clear but all i want to know is where is the best place to put the code like above to insert a image in the header?

0 likes
1 reply

Please or to participate in this conversation.