代码之家  ›  专栏  ›  技术社区  ›  davidvera

与Laravel 5.5登录后传递共享变量

  •  0
  • davidvera  · 技术社区  · 8 年前

    我创建了一个方法,以便与应用程序的所有视图共享数据。

    为此,我创建了一个类entityrepository,在该类中存储要与所有视图共享的数据。

    这些数据显示在布局中,而不是视图中。

    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;
        }
    }
    

    当我想向方法发送数据时,只需调用getEntities()方法…例如:

    public function index(EntityRepository $vcs)
    {
        $entitiesLists = $vcs->getEntities();
    
        // My code here ...
        return view('admin.pages.maps.sectors.index', compact('entitiesLists', 'myVars'));
    }
    

    在这种特定的情况下,它工作得很好,我没有问题。我的问题是登录后的登录页面。

    在LoginController中:

    我这样定义了重定向到变量:

    public $redirectTo = '/admin/home';
    

    出于特定原因,我必须重写LoginController中的authentificated()方法,以检查我的应用程序是否已配置或需要设置…

    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');
        }
    }
    

    相关的index()方法正在将变量发送到视图中:

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

    无论我返回什么,我都会收到错误消息…

    未定义的变量:entitieslist(视图:C:\wamp64\www\network dev\resources\views\admin\partials\header hor menu.blade.php)

    1 回复  |  直到 8 年前
        1
  •  0
  •   davidvera    8 年前

    我最终通过改变路线解决了这个问题:

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

    在我的LoginController中,我更改了:

    public $redirectTo = '/admin/home';
    

    到:

    public $redirectTo = '/admin/main';
    

    最后:

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