Search code examples
model-view-controllerlaravel-5.5

Passing shared variable after login with Laravel 5.5


i created a method in order to share datas with all views of my application.

For this i created a class EntityRepository where i store the datas I want to share with all views.

Those data are displayed in the layout NOT the view.

class EntityRepository
{
    use App\Valuechain;

    public function getEntities()
    {
        $vcs = Valuechain::select('valuechains.id', 'lang_valuechain.vcname', 'lang_valuechain.vcshortname')
            ->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
            ->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
            ->where('langs.isMainlanguage', '=', '1')
            ->whereNull('valuechains.deleted_at')
            ->get();
        return $vcs;
    }
}

When I want to send datas to the methods I simply call the getEntities() method... For example :

public function index(EntityRepository $vcs)
{
    $entitiesLists = $vcs->getEntities();

    // My code here ...
    return view('admin.pages.maps.sectors.index', compact('entitiesLists', 'myVars'));
}

In this specific case it works fine and i don't have issue. My issue concerns the landing page after login.

In the loginController :

I defined the redirectTo variable this way :

public $redirectTo = '/admin/home';

For specific reasons I had to override the authentificated() method in the LoginController in order to check if my app is configured or need to be setup ...

protected function authenticated(Request $request, $user)
{

    $langCount = Lang::count();
    if ($langCount == 0) {
        return redirect()->to('admin/setup/lang');
    }
    else {
        //return redirect()->to('admin/home');
        return redirect()->action('BackOffice\StatsController@index');
    }
}

The concerned index() method is sending the variable onto the view :

public function index(EntityRepository $vcs)
{
    $entitiesLists = $vcs->getEntities();
    return view('admin.home', compact('entitiesLists'));
}

Whatever the return i make i have error message...

Undefined variable: entitiesLists (View: C:\wamp64\www\network-dev\resources\views\admin\partials\header-hor-menu.blade.php)


Solution

  • I finally solved this issue by changing my routes :

    Route::group(['prefix' => 'admin'], function () {
        Route::get('/', function (){
            $checkAuth = Auth::guard('admin')->user();           
            if ($checkAuth) {
                return redirect('/admin/main');
            }
            else {
                return redirect('admin/login');
            }
        });
    });
    

    In my loginController i changed :

    public $redirectTo = '/admin/home';
    

    to :

    public $redirectTo = '/admin/main';
    

    Finally :

    protected function authenticated(Request $request, $user)
    {
    
        $langCount = Lang::count();
    
        if ($langCount == 0) {
            return redirect()->to('admin/setup/lang');
        }
        else {
            return redirect()->to('admin/main');
        }
    }