Laravel 4检查是否已登录(javascript) [英] Laravel 4 Check if logged in (javascript)

查看:77
本文介绍了Laravel 4检查是否已登录(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上

我有一个(也许是简单的问题),我不知道该怎么做.我正在使用Laravel Framework版本4.

I have one (maybe simple question), which I can't figure out how to do on my own. I'm using Laravel Framework version 4.

如何检查用户是否在javascript中登录?我希望菜单栏在用户向下滚动100px时淡出,但是如果用户登录,那么我希望菜单栏在用户向下滚动150px时淡出.

How can I check if a user is logged in within javascript? I want the menu bar to fadeout when a user scrolls 100px down, but if the user is logged in then I want it to fadeout when the user scrolls 150px down..

这是现在的javascript:

This is how the javascript looks like by now:

(function ($) {
$(document).ready(function(){

$(window).scroll(function() {
    var yPos = ( $(window).scrollTop() );
    if(yPos > 100) { // show sticky menu after screen has scrolled down 200px from the top
        $("#fixed_header").fadeIn(100);
        $("#fixed_header_konto").fadeIn(100);
        $("#header").fadeOut(100);
        $("#header_konto").fadeOut(100);
    } else {
        $("#fixed_header").fadeOut(100);
        $("#fixed_header_konto").fadeOut(100);
        $("#header").fadeIn(100);
        $("#header_konto").fadeIn(100);
    }
    });
  });
}(jQuery));

推荐答案

您可以使用view composer将登录的用户数据(您也可以只传递一个标志)直接转储到JavasScript中,方法如下: :

You may use a view composer to dump the logged in user data (you may just pass a flag either) directly to the JavasScript using something like this:

View::composer('layouts.master', function($view) {
    $user = null;
    if(Auth::check()) {
        $user = Auth::user();
    }
    $view->with('authUser', $user);
});

您的主版面:

<!DOCTYPE html>
<html lang="en">
    <head>
        <script>var userObj = {{ $authUser or 'undefined' }}</script>
    </head>
    <body>
        <!-- content -->
    </body>
</html>

现在,您可以使用以下方法检查用户是否已登录:

Now, you may able to check if the user is logged in or not by using something like this:

<script>
  $(function(){
    if(userObj) console.log(userObj);
  });
</script>

这是我上次于3月6th March, 2014 Heera.IT 上撰写的文章,请查看该文章以获取更详细的解释.另外,您可能不需要转储整个user object,但这很好.

This is from my last article written on march 6th March, 2014 on Heera.IT, check the article for more detailed explanation. Also, you may don't need to dump the whole user object but it's fine.

这篇关于Laravel 4检查是否已登录(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆