在一个动作中,我想通过重定向将发送参数发送到另一个动作。
因此,当我在文档(http://book.cakephp.org/3.0/en/controllers.html#redirecting-to-other-pages)中阅读此文件时:
return $this->redirect(['action' => 'edit', $id]);
Run Code Online (Sandbox Code Playgroud)
我写:
public function activate($user_id, $token) {
//.... of course $user->username is not null here
return $this->redirect(['action' => 'login', $user->username]);
//....
}
Run Code Online (Sandbox Code Playgroud)
在我的目标动作中:
public function login($username = null) {
//...
debug($username); // just displays null
//...
}
Run Code Online (Sandbox Code Playgroud)
我不知道问题是在redirect()还是在login()中。无论如何,debug($ this-> request)不会显示任何传递的参数。
问候,
在CakePHP 2中我可以通过使用获取当前操作$this->action,但在CakePHP 3.x中我不能再使用它了,因为它返回以下错误:
Error: actionHelper could not be found.
Run Code Online (Sandbox Code Playgroud)
如何在CakePHP 3中获取当前操作?
在我的行为中我需要调用者模型来查找和保存数据.我怎么能做这样的事情.
class MyBehavior extends Behavior {
public function func() {
$entities = $this->find()->all();
}
Run Code Online (Sandbox Code Playgroud) 下面的功能是为了确保来自表单的电子邮件是唯一的,如果它已经在使用中,则会显示一条消息.我想改变这个消息.
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['username']));
$rules->add($rules->isUnique(['email']));
return $rules;
}
Run Code Online (Sandbox Code Playgroud)
我试过这种方式:
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['username']));
$rules->add($rules->isUnique(['email']),
['errorField' => 'email', 'message' => 'Este email já encontra-se em uso.']
);
return $rules;
}
Run Code Online (Sandbox Code Playgroud)
它有效,但两个消息都显示,默认和我的.
我使用cakePHP 3,我在Form-> input()中有一个符号.如果故意我犯了错误,这个错误不会在表单字段下出现.它不会出现在任何地方.
我的代码是这样的:
$newUser = $this->Users->newEntity($this->request->data());
if (!$this->Users->save($newUser)) {
debug($newUser->errors());
$this->Flash->error('Error');
return;
}
Run Code Online (Sandbox Code Playgroud)
Debug显示错误,但是它们不应该自动出现在每个表单元素下面吗?
使用CakePHP 3.4,PHP 7.0.
我正在尝试使用一个非常简单的控制器方法来输出一些JSON.它输出"无法修改标题...".
public function test() {
$this->autoRender = false;
echo json_encode(['method' => __METHOD__, 'class' => get_called_class()]);
}
Run Code Online (Sandbox Code Playgroud)
浏览器输出
{"method":"App\\Controller\\SomeController::test", "class":"App\\Controller\\SomeController"}
Warning (512): Unable to emit headers. Headers sent in file=...
Warning (2): Cannot modify header information - headers already sent by (output started at ...)
Warning (2): Cannot modify header information - headers already sent by (output started at ...)
Run Code Online (Sandbox Code Playgroud)
我完全理解为什么PHP抱怨这个.问题是为什么CakePHP会抱怨我该怎么办?
应该注意的是,CakePHP 2.x允许这样做.
在 cakePHP 3.4 上,我有 3 个表,其中一个属于并且有很多关系:成分、产品和成分产品:
class IngredientsTable extends Table
{
public function initialize(array $config)
{
// Use through option because it looks like you
// have additional data on your IngredientsProducts table
$this->belongsToMany('Products', [
'through' => 'IngredientsProducts',
]);
}
}
class ProductsTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Ingredients', [
'through' => 'IngredientsProducts',
]);
}
}
class IngredientsProductsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Ingredients');
$this->belongsTo('Products');
}
}
Run Code Online (Sandbox Code Playgroud)
我想要完成的是,当我插入一个新产品时,我还想插入与该产品相关的每种成分的成分 ID 和字段数量,在成分产品连接表上。
我一直在阅读食谱,看到在 joiner …
cakephp save associations has-and-belongs-to-many cakephp-3.x
我有问题在Cakephp 3.5.x中使用cookie.
在早期版本中,我使用了Cookie组件,但现在已弃用.我不清楚如何使用这个新的中间件来读写cookie.
该文档还不清楚我.它向我展示了如何设置cookie中间件,而不是如何处理在控制器中创建cookie.是否有人在3.5.x中处理过cookie?
我正在将CakePHP数组的返回值转换为JSON,目前它是这样的:
{
"platformusers" : [
{
"id" : "1",
"name" : "user1"
},
{
"id" : "3",
"name" : "user3"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我希望它是这样的:
[
{
"id" : "1",
"name" : "user1"
},
{
"id" : "3",
"name" : "user3"
}
]
Run Code Online (Sandbox Code Playgroud)
我正在努力 Set :: extract('{n} .Model',$ data) Hash :: extract('{n} .Model',$ data)根本没有运气.
完整代码:
$platformusers = $this->Platformuser->find('all', array(
'fields' => array('Platformuser.id', 'Platformuser.name')
));
$platformusers = Hash::extract('{n}.Platformuser', $platformusers);
$this->set(array(
'platformusers' => $platformusers,
'_serialize' => array('platformusers')
));
Run Code Online (Sandbox Code Playgroud) CakePHP 2.3 在 core.php 文件中设置会话变量(包括 cookie 属性)。我需要为会话 cookie 设置samesite=None和Secure=true,但配置中似乎没有这些设置,仅显示以下选项:
Session.cookie- 要使用的 cookie 的名称。默认为“CAKEPHP”Session.timeout- 您希望会话持续的分钟数。这个超时由 CakePHP 处理Session.cookieTimeout- 您希望会话 cookie 存活的分钟数。Session.checkAgent- 您希望在启动会话时检查用户代理吗?在处理旧版本的 IE、Chrome Frame 或某些网络浏览设备和 AJAX 时,您可能需要将该值设置为 falseSession.defaults- 用作会话基础的默认配置集。有四个内置函数:php、cake、cache、database。Session.handler- 可用于启用自定义会话处理程序。需要一个可调用数组,可以与session_save_handler. 使用此选项将自动添加session.save_handler到 ini 数组中。Session.autoRegenerate- 启用此设置,打开会话自动更新以及经常更改的sessionid。请参阅 CakeSession::$requestCountdown。Session.ini- 要设置的附加 ini 值的关联数组。这就是我现在的情况:
Configure::write('Session', array(
'defaults' => 'database',
'handler' => array('model' => 'cake_sessions'),
'timeout' => 60
));
Run Code Online (Sandbox Code Playgroud)
有解决方法吗?我一直在研究如何使用 php 执行此操作,但我不确定如何编辑 CakePHP 使用我想要的属性创建的会话 cookie,或者在创建 …
cakephp ×10
cakephp-3.0 ×6
php ×3
cakephp-3.x ×2
cookies ×2
validation ×2
arrays ×1
associations ×1
cakephp-2.3 ×1
cakephp-2.x ×1
cakephp-3.4 ×1
controller ×1
format ×1
forms ×1
httpresponse ×1
json ×1
middleware ×1
samesite ×1
save ×1