发布将facebook数据传递给来自routes.php的View

时间:2014-09-05 16:48:31

标签: facebook facebook-graph-api laravel laravel-4

您好我在view laravel-4中尝试显示数据时收到此错误消息。我正在使用Facebook SDK dev-master

这是我的error:

Trying to get property of non-object (View: C:\wamp\www\irishrail.tld\app\views\user\authed.blade.php) 

这是我的view:

<div class="cover authed">
        <div class="profile">
            <div class="avatar">
                <img height="100%" width="100%" src="{{$cred->profile_image_url}}" alt="">
            </div>
        </div>
    </div>
    <div class="title-bar">
        Congrats {{$cred->name}}
    </div>
    <div class="content">
        <form action="/" method="post">
            <input type="text" name="code" class="field" placeholder="Redemption Code">
            <button class="btn btn-green">Claim your ticket</button>
        </form>
    </div>

这是我的route:

Route::get('login/facebook/callback', function(){
    $code = Input::get('code');//error

    $facebook = new Facebook(Config::get('facebook'));
    $uid = $facebook->getUser();//Gets the user id

    //Get User Details from FB
    $credentials = $facebook->api('/me');
    $photo = 'http://graph.facebook.com/'.$uid.'/picture';

    $credentials['name'] = $credentials['first_name']." ".$credentials['last_name'];
    $credentials['profile_image_url'] = $photo;//Add photo to the credentials

    return View::make('user.authed')->with('cred', $credentials);
});

1 个答案:

答案 0 :(得分:0)

在您的路线中,您将$credentials定义为数组,在您的视图中,您将通过对象运算符访问它。

{{ $cred['name'] }}

应该在您看来适合您。

另一种选择是将数组转换为对象。在您的路线中,在返回视图之前,请执行

$credentials = (object) $credentials;

然后您就可以访问视图中的数据,如上例所示。

顺便说一下,这与facebook sdk或laravel无关,这是一个简单的php错误。