Laravel。如何获得/通过登录用户ID在骨干网 [英] Laravel. How to get/pass logged in users ID in Backbone

查看:1858
本文介绍了Laravel。如何获得/通过登录用户ID在骨干网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个系统,Laravel和骨干。

用户可以查看自己的个人资料如电子邮件地址,姓名,出生日期等。

在Laravel我使用的资源控制器(索引,创建,存储,显示,编辑,更新和销毁)

因此​​,如果用户想要查看自己的个人资料,他们会去domain.com/users/{their~~V ID}

我遇到的问题是我怎么传递{其ID}骨干,所以我可以追加到域名,所以当骨干取它可以获取他们的纪录。

骨干工作正常。如果我硬code中的ID它会抢了正确的数据。

用户ID当前存储在会话中。

什么是最好的做法吗?你是如何想送骨干用户ID。

骨干型号/ profile.js

  VAR外形= Backbone.Model.extend({
     urlRoot:域名+/用户,
    初始化:功能(){
        this.profile =新ProfileCollection();
    }
}),ProfileCollection = Backbone.Collection.extend({    初始化:功能(机型,选择= {}){
        this.id = options.id;
    },
    网址:函数(){
        返回域+/用户/+ this.id;
    },
    型号:个人资料,});
返回{
    简介:简介,
    ProfileCollection:ProfileCollection
}

骨干查看/ profile.js

  VAR ProfileView = Backbone.View.extend({    EL:'。第',
    初始化:功能(){
        自=这一点;
        外形=新Profile.Profile;
        profileCollection =新Profile.ProfileCollection([] {ID:453445});
    },
    渲染:功能(){        profileCollection.fetch({
            成功:函数(){
            }
        });
    },


解决方案

在您的视图( Laravel查看)做这样的事情:

  @if(AUTH ::检查())
    <脚本>
        VAR用户ID ={{验证::用户() - GT; ID}};
    < / SCRIPT>
@万一

现在你可以在你的的JavaScript 使用用户ID 。你也可以创建一个 View Composer的

  //你可以保持这个code在filters.php文件
查看::作曲家('layouts.master',函数($视图){
    $ USER = NULL;
    如果(AUTH ::检查()){
        $ USER =验证用户::() - GT;的toJSON();
    }
    $视图 - >在('userJs',$用户);
});

在你的布局,之间的< HEAD>

 <脚本> VAR userObj = {{$ userJs或未定义}}< / SCRIPT>

所以,你可以随时使用JS中的用户对象:

 如果(userObj){
    的console.log(userObj.id);
    的console.log(userObj.username);
    的console.log(userObj.email); //所有属性
}

更新:

您可以省略的toJSON() $ USER =验证::用户() - GT;的toJSON(); Laravel 将做到这一点。

I'm developing a system in Laravel and Backbone.

The user can view their own profile eg, email address, Name, DOB, etc

In Laravel I am using the Resource Controller (index, create, store, show, edit, update and destroy)

So if the user would like to view their own profile they would go to domain.com/users/{their ID}.

The issue I am having is how do I pass {their ID} to backbone so I can append it to the domain name so when backbone fetches it can fetch their record.

Backbone is working fine. If I hardcode the id in it will grab the correct data.

The user ID is currently stored in the session.

What is the best practice here? How are you suppose to send backbone the User ID.

Backbone Model/profile.js

    var Profile = Backbone.Model.extend({   
     urlRoot: domain + "/users",
    initialize: function () {
        this.profile = new ProfileCollection();
    }   
}),

ProfileCollection = Backbone.Collection.extend({

    initialize: function(models, options = {}) {
        this.id = options.id;
    },
    url: function() {
        return domain + "/users/" + this.id;
    }, 
    model: Profile,

});


return {
    Profile: Profile,
    ProfileCollection: ProfileCollection
}

Backbone View/profile.js

    var ProfileView = Backbone.View.extend({

    el: '.page',
    initialize: function() {
        self = this;        
        profile = new Profile.Profile;  
        profileCollection = new Profile.ProfileCollection([], { id: 453445 });
    },


    render: function(){

        profileCollection.fetch({
            success : function(){       
            }
        });
    },

解决方案

In your view (Laravel View) do something like this:

@if(Auth::check())
    <script>
        var userID = "{{ Auth::user()->id }}";
    </script>
@endif

Now you can use the userID in your JavaScript. You may also create a View Composer:

// You may keep this code in your filters.php file
View::composer('layouts.master', function($view) {
    $user = null;
    if(Auth::check()) {
        $user = Auth::user()->toJson();
    }
    $view->with('userJs', $user);
});

In your master layout, between <head>:

<script>var userObj = {{ $userJs or 'undefined' }}</script>

So, you can always use the user object in js:

if(userObj) {
    console.log(userObj.id);
    console.log(userObj.username);
    console.log(userObj.email); // all properties
}

Update:

You can omit the toJson() from $user = Auth::user()->toJson();. Laravel will do it.

这篇关于Laravel。如何获得/通过登录用户ID在骨干网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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