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

请问为什么取不到$wechat呢 #2

Closed
snoywing opened this issue Apr 19, 2015 · 16 comments
Closed

请问为什么取不到$wechat呢 #2

snoywing opened this issue Apr 19, 2015 · 16 comments

Comments

@snoywing
Copy link

$wechat = App::make('wechat');
$wechat->on('message', ...);

在laravel 5 中报错 :[2015-04-19 03:19:04] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Undefined class constant 'User'' in /var/www/wechat/app/Http/Controllers/WechatController.php:22

请问这是为什么呢?

@overtrue
Copy link
Owner

请贴一下 WechatController.php:22 的代码。

@snoywing
Copy link
Author

$wechat = App::make('wechat');

@overtrue
Copy link
Owner

贴一下整个控制器代码

@snoywing
Copy link
Author

items(function(){ return array( Message::make('news_item')->title('欢迎您,第'.$wechat->user->all($nextOpenId = null).'位关注者')->picUrl('http://www.baidu.com/images/1.jpg'), Message::make('news_item')->title('社保案例')->description('好不好?'), Message::make('news_item')->title('社保照片')->description('好不好说句话?')->url('http://baidu.com'), Message::make('news_item')->title('社保生活')->url('http://baidu.com/abc.php')->picUrl('http://www.baidu.com/demo.jpg'), Message::make('news_item')->title('社保代缴')->url('http://baidu.com/abc.php')->picUrl('http://www.baidu.com/demo.jpg'), ); }); }); return Wechat::serve(); } ``` } 请问安安,这样写对吗?这样的业务逻辑不就都在server这个function下了?

@overtrue
Copy link
Owner

<?php

$foo = 'bar';
$hello = 'world';

Wechat::on('event', 'subscribe', function($event){
  // 这里是取不到变量$foo、$hello的
});

除非:

Wechat::on('event', 'subscribe', function($event) use ($foo, $hello, ... 其它你要在function内部用的变量){
   //这里就可以使用use到的变量了,$foo,...
});

请参考:http://php.net/manual/zh/functions.anonymous.php

image

@snoywing
Copy link
Author

现在的问题是app::make的时候报错了。在controller下面那行,安安没遇到过这样的问题吗

@overtrue
Copy link
Owner

@snoywing 没有,你先按我上面的写法纠正你的:

Message::make('news_item')->title('欢迎您,第'.$wechat->user->all($nextOpenId = null).'位关注者')->picUrl('http://www.baidu.com/images/1.jpg'), 

里的$wechat变量问题再试试看。

@overtrue
Copy link
Owner

另外:

$wechat->user->all($nextOpenId = null)

返回的是数组,不是数字。。。

@snoywing
Copy link
Author

好的,收到

@snoywing
Copy link
Author

还是不行,问题是$wechat 获取不到。报[2015-04-19 04:30:38] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'App\Http\Controllers\App' not found' in /var/www/wechat/app/Http/Controllers/WechatController.php:15
就是这行

$wechat = App::make('wechat');

@overtrue
Copy link
Owner

... 😞

命名空间的使用,不能命名空间需要引入:

use App;

然后才能

App::make

@snoywing
Copy link
Author

OK,结贴结贴,3q,安安

@overtrue
Copy link
Owner

@snoywing 不报错了?

@snoywing
Copy link
Author

嗯,搞定了。 我以为在Illuminate 中加入以后这里就不用引用了呢。

@snoywing
Copy link
Author

谢谢安安啦

@overtrue
Copy link
Owner

不客气,这些是PHP的语法问题,与框架无关,所以在任何地方用PHP命名空间都一样

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