Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RafaelMunoznl's avatar

Different variables behaviour in localhost vs deployed

In my application I need to make globally available four variables: $user, $employee, $role and $company

In order to show different menu options and landing views after login.

I do this in my Controller.php

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if (Auth::user()) {
                $user = Auth::user();
                $employee = $user->employee;
                $role = $user->role->slug;
                $company = $user->company;
                view()->share('signed_in', Auth::check());
                view()->share('user',  $user);
                view()->share('employee',  $employee);
                view()->share('role',  $role);
                view()->share('company',  $company);
            }
            return $next($request);
        });
    }
}

This work as expected in my development local enviroment (Localhost).

Now I deployed my app (first time I deploy a Laravel app) in CloudWays and it does ont work as expected.

It turns out that Auth::user() is empty (not defined and therefore it breaks the app.

I double checked the login info (email and password) and they are right.

It is the same code, that I pushed to git and deployed from there.

Any idea, why Auth::user() is never defined? What Am I missing?

0 likes
10 replies
masumluf's avatar

View share only for sharing data berween blade file not in the controller.

RafaelMunoznl's avatar

@masumluf sorry i do not understand what you mean.

This exact code works in Localhost but not deployed. How do I make available a variable globally in my appliation? (Controller and views)

Snapey's avatar

@masumluf that is not correct

@rafaelmunoznl your problem is probably that the user is not actually logged in.

This can be cause by some unwanted output being sent to the browser before any php code output. Are you running apache in cloudways?

To tell if you have problems with cookies (which causes the user to not stay logged in) check in your browser if cookies are being set. You should see a laravel session cookie.

RafaelMunoznl's avatar

@snapey Yes, Cookies are set in my Browser. I see the laravel session cookie:

laravel_session : "eyJpdiI6IjNSZGpub0lXYlo3RW03RHhE..... "

Yes, I am running apache in Cloudways (I think)

Snapey's avatar
Snapey
Best Answer
Level 122

Can you add something to check if the user is logged in? add a

@dump(Auth()->user()->name ??  'not logged in')

somewhere prominent in your master layout.

By the way, adding your code to every controller is not the correct approach. You should consider a view composer for information shared with every view.

RafaelMunoznl's avatar

@snapey once I try to log in I get the following error:

Undefined variable: user (View: /home/363286.cloudwaysapps.com/ccsnbfpybb/public_html/resources/views/partials/menues/employee.blade.php) ...

As I understand, I am not able to log in

To your last comment: I am adding that code just once in the Controller.php.

Snapey's avatar

You may have to comment that code out whilst you work out what is going on.

RafaelMunoznl's avatar

@snapey Thanks for your explanation. Based on it, I just eliminated the code from the controller and buidl the same logic in a ViewComposer in the AppServiceProvider like this:

 public function boot()
    {


        View::composer('*', function ($view) {
            if (Auth::user()) {
                $user = Auth::user();
                $employee = $user->employee;
                $role = $user->role->slug;
                $company = $user->company;
                $view->with('signed_in', Auth::check());
                $view->with('user',  $user);
                $view->with('employee',  $employee);
                $view->with('role',  $role);
                $view->with('company',  $company);
            }
        });
    }

And now it works everything as expected

Snapey's avatar

Thats great. Just check it with Laravel debugbar or similar, because * in the view composer will add for every view - including @includes. So if you have partials being injected into views, the code will be rendered every time.

RafaelMunoznl's avatar

@snapey Added in each @include is definitively not good.

But anyway, I have a long way in front of me making refactors and so on.

That my (first) app is working online as expected is already a big step for me. Thanks for your help

Please or to participate in this conversation.