Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于Bag #119

Closed
iam993 opened this issue Oct 14, 2015 · 11 comments
Closed

关于Bag #119

iam993 opened this issue Oct 14, 2015 · 11 comments

Comments

@iam993
Copy link

iam993 commented Oct 14, 2015

用户验证过后,返回的是一个Bag对象,请问怎么使用这个对象。
另外,在getUser里return new Bag($this->http->get($url));
为什么不直接写为return $this->http->get($url);呢?

@overtrue
Copy link
Collaborator

@iam993
Copy link
Author

iam993 commented Oct 14, 2015

应该是说网页授权
$this->http->get($url)返回的就是授权用户的信息,包括昵称等。
但是return new Bag($this->http->get($url));后我就不知道怎么用了。
也就是你给你例子:
if (empty($_SESSION['logged_user'])) {
$user = $auth->authorize(); // 返回用户 Bag
$_SESSION['logged_user'] = $user;
// 跳转到其它授权才能访问的页面
}

$user = $_SESSION['logged_user'] ;
不知道这个$user怎么取数据。

@overtrue
Copy link
Collaborator

Bag的用法与普通类用法一样。上面已经给了你例子了

@iam993
Copy link
Author

iam993 commented Oct 14, 2015

我用$Bag->all()的时候,老是报类似什么serialize __autoload_的错误。
而且上面已经use Overtrue\Wechat\Utils\Bag过了。纠结了一下午。

@overtrue
Copy link
Collaborator

@iam993 请贴错误截图

@iam993
Copy link
Author

iam993 commented Oct 15, 2015

:(
Home\Controller\UserController::Login(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Overtrue\Wechat\Utils\Bag" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition

--------------------------------------------------------上面是错误,下面是代码
use Think\Controller;
use Overtrue\Wechat\Utils\Bag;
use Overtrue\Wechat\User;

class UserController extends Controller {
public function Login(){
if( !isset($_SESSION['user']) ){
$Auth = new Auth(C('appid'),C('appsecret'));
$_SESSION['user'] = ($Auth->authorize());
}else{
$user = ($_SESSION['user']);
var_dump(($user->all()));

    }
}

public function LoginOut(){
    unset($_SESSION["user"]);
    echo 'loginout!';
}

}

@overtrue
Copy link
Collaborator

SESSION 不支持对象存储吧?支持数组与标量。所以你直接把 $auth->authorize() 存进去就是错的吧?

$user = ($_SESSION['user']); // 你这里得到的要么是数组要么是标量
var_dump(($user->all())); // 然而你却把它当成Bag对象用,谁教你的呢?

@iam993
Copy link
Author

iam993 commented Oct 15, 2015

https://github.com/overtrue/wechat/wiki/%E7%BD%91%E9%A1%B5%E6%8E%88%E6%9D%83

// 请一定要自己存储用户的登录信息,不要每次都授权
if (empty($_SESSION['logged_user'])) {
$user = $auth->authorize(); // 返回用户 Bag
$_SESSION['logged_user'] = $user;
// 跳转到其它授权才能访问的页面
}

var_dump($_SESSION['logged_user']);

我是看了你的例子,我以为可以这样用。那要怎么用呢?就我这里例子。

@overtrue
Copy link
Collaborator

@iam993 请学习PHP基础

@iam993
Copy link
Author

iam993 commented Oct 15, 2015

懂了,大脑生锈,我把session想成c#的了,才学这个语言,还有点不熟。
谢谢了!代码写的真不错,我这样的新手也能看懂。
另外,有没有想过做公众号第三方的开发?

@overtrue
Copy link
Collaborator

@iam993 没想过,上班太忙

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants