我正在尝试使用CakePHP v2.1 +中的事件系统
它看起来非常强大,但文档有些模糊.触发事件看起来很简单,但我不知道如何注册相应的监听器来监听事件.相关部分在这里,它提供以下示例代码:
App::uses('CakeEventListener', 'Event');
class UserStatistic implements CakeEventListener {
public function implementedEvents() {
return array(
'Model.Order.afterPlace' => 'updateBuyStatistic',
);
}
public function updateBuyStatistic($event) {
// Code to update statistics
}
}
// Attach the UserStatistic object to the Order's event manager
$statistics = new UserStatistic();
$this->Order->getEventManager()->attach($statistics);
Run Code Online (Sandbox Code Playgroud)
但它没有说明这个代码应该驻留在哪里.在特定的控制器内?在app控制器里面?
如果它是相关的,那么监听器将成为我正在编写的插件的一部分.
更新: 这听起来像一种流行的方法是将监听器注册码放在插件的bootstrap.php文件中.但是,我无法弄清楚如何从那里调用getEventManager(),因为应用程序的控制器类等不可用.
更新2: 我还被告知听众可以住在模特里面.
更新3: 最后一些牵引力!以下代码将在MyPlugin/Config/bootstrap.php中成功记录事件
App::uses('CakeEventManager', 'Event');
App::uses('CakeEventListener', 'Event');
class LegacyWsatListener implements CakeEventListener {
public function implementedEvents() {
return array(
'Controller.Attempt.complete' => 'handleLegacyWsat',
);
} …Run Code Online (Sandbox Code Playgroud) 我能够启动Twitter应用程序(如果它存在于手机上),但我找不到如何自动显示特定的用户配置文件.
这样的内容适用于Google+:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.google.android.apps.plus", "com.google.android.apps.plus.phone.UrlGatewayActivity");
intent.putExtra("customAppUri", "USER_ID");
Run Code Online (Sandbox Code Playgroud)
这是Facebook的方式:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/USER_ID"));
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
当我启动Twitter应用程序时应该有相同类型的解决方案?
Intent intent = getPackageManager().getLaunchIntentForPackage("com.twitter.android");
intent.putExtra("WHAT_TO_PUT_HERE?", "USER_ID");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud) 有没有办法通过引用lambda函数传递当前在范围内的任何变量而不在use(...)语句中列出它们?
就像是
$foo = 12;
$bar = 'hello';
$run = function() use (&) {
$foo = 13;
$bar = 'bye';
}
// execute $run function
Run Code Online (Sandbox Code Playgroud)
导致$foo等于13和$bar等于'bye'.
的结果:
var_dump(null != $a = 15);
var_dump($a);
Run Code Online (Sandbox Code Playgroud)
是:
bool(true)
int(15)
Run Code Online (Sandbox Code Playgroud)
为什么这个脚本不会触发错误?由于!=(不等于运算符)的优先级高于=(赋值运算符),$a应该与null第一个进行比较?
我如何强制CakePHP 2.x检索tinyint数据库列不是布尔值而是tinyint?
MySQL的:
Column | type
-------------------------------
... | ...
category_id | tinyint(1)
... | ...
Run Code Online (Sandbox Code Playgroud)
CakePHP的:
$this->request->data = $this->Question->read();
var_dump($this->request->data['Question']['category']);
Run Code Online (Sandbox Code Playgroud)
值始终为0(如果我提取的问题为类别ID 0)或1(如果问题具有任何其他类别ID).
像移动应用程序一样,Facebook已经发布了动作.
正如文件所说:
To publish a built-in Like action on an Open Graph object,
invoke the following HTTP POST request with a user’s access
token and the url of the Open Graph object.
This Open Graph object can be of any type.
curl -X POST \
-F 'access_token=USER_ACCESS_TOKEN' \
-F 'object=OG_OBJECT_URL' \
https://graph.facebook.com/[User FB ID]/og.likes
Run Code Online (Sandbox Code Playgroud)
通常,为了提出例如用户信息的请求,我使用这种代码:
Facebook facebook;
facebook = new Facebook(AppConfig.FACEBOOK_APP_ID);
if (facebook.isSessionValid())
{
JSONObject obj = facebook.request("me");
...
}
else
{
facebook.authorize(...)
{
@Override public void onComplete(Bundle values)
{ …Run Code Online (Sandbox Code Playgroud) php ×4
android ×2
cakephp ×2
cakephp-2.0 ×1
cakephp-2.1 ×1
events ×1
lambda ×1
operators ×1
parsing ×1
twitter ×1