天道酬勤,学无止境

cakephp-3.1

Is it possible to use AppController on error pages? (Cakephp 3.1)

I am trying to render error templates (eg error400.ctp) but with the default layout (site header and footer) which relies on components and variables set in AppController. How do I tell Cake to use AppController when rendering error pages? I have already tried making an ErrorController which extends AppController, but it breaks for missing actions.

2022-01-18 17:53:19    分类:问答    cakephp   cakephp-3.0   cakephp-3.1

用户未登录时如何使用 AJAX 和 CakePHP 3 检查 CSRF 令牌?(How to check CSRF token using AJAX and CakePHP 3 when user is not logged in?)

问题 所以我让我的网站上线了,我进入了人们并不总是友善的公共领域。 我刚开始学习 CSRF,当我让我的 cakephp 3 网站上线时,我发现它是我需要的东西。 如这里所见! 我将 csrf 组件和安全组件添加到我的站点,但我有 1 个主要问题。 现在,当用户想要注册时,他们不能。 我使用自定义表单进行条纹发送付款,但还将使用 ajax 的用户添加到我的数据库中。 首先添加用户,然后处理付款并将订单也保存到数据库中。 根据条带文档,我在单击提交按钮后将隐藏值中的令牌添加到表单中,并且不禁注意到我的新安全性不允许这种情况发生。 由于我使用 ajax 将发布数据发送到我的用户控制器并在提交时添加表单输入, 如何检查 csrf 令牌并确保不存在安全漏洞而不禁用所涉及操作的安全性? 非常感谢如何完成此操作的示例,因为在 cakephp 3 中似乎缺少执行此操作的示例。我也很难弄清楚一切是如何工作的,因为 cakephp 3 automagic 将令牌添加到表单和曲奇饼。 我不确定如何/在哪里/检查什么。 回答1 要传递X-CSRF-Token ,请在您的 Ajax 请求中使用beforeSend参数,并定义 cookie 的csrfToken值。 $.ajax({ url: '/foo/bar', type: 'POST', dataType: 'HTML', data: data

2022-01-13 23:16:57    分类:技术分享    cakephp   cakephp-3.0   cakephp-3.1   cakephp-3.x

How to check CSRF token using AJAX and CakePHP 3 when user is not logged in?

So I made my site live and I am entering into the public realm where people aren't always nice. I just started learning about CSRF and saw that it was something I needed when I made my cakephp 3 site live. As seen here! I added the csrf component and the security component to my site, but I have 1 major problem. Now, when users want to sign up they can't. I use a custom form for stripe to send payment, but also add a user using ajax to my database. The user gets added first and then the payment is processed and saves the order to the database as well. According to stripe docs I add the token

2022-01-13 19:15:56    分类:问答    cakephp   cakephp-3.0   cakephp-3.1   cakephp-3.x

cakephp3 会话已弃用(cakephp3 session deprecated)

问题 我已将最新版本的 cakephp 核心更新到我的项目中。 现在我收到这个错误: Deprecated (16384): SessionHelper has been deprecated. Use request->session() instead. [CORE/src/View/Helper/sessionHelper.php, line 39] 我认为的错误在这一行: if ($this->session->read('admin_logged_in')){ 我应该用什么代替? 谢谢 回答1 if ($this->request->session->read('admin_logged_in')){ 应该没事。 更新 根据下面@raph 的评论,正确答案是 $this->request->session()->read('admin_logged_in') 注意会话后的 () 回答2 在 Cakehp 3.7 中,改为使用 getSession()。 $this->request->getSession()->read('admin_logged_in'); 参考:https://book.cakephp.org/3.0/en/development/sessions.html

2021-12-12 09:10:44    分类:技术分享    php   session   cakephp   cakephp-3.1

cakephp3 session deprecated

I have update into my project the last version of cakephp core. Now I get this error: Deprecated (16384): SessionHelper has been deprecated. Use request->session() instead. [CORE/src/View/Helper/sessionHelper.php, line 39] The error I think is in this line: if ($this->session->read('admin_logged_in')){ What should I use instead? Thanks

2021-11-26 00:26:33    分类:问答    php   session   cakephp   cakephp-3.1

CakePHP 3.1:翻译行为字段的验证 (i18n)(CakePHP 3.1: Validation for translate behaviour fields (i18n))

问题 我正在尝试使用 CakePHP 翻译行为以一种形式添加包含多个翻译的项目。 如何验证翻译字段? 例如,需要特定的语言吗? 假设您有一个简单的items表和一个单独的翻译表items_i18n ,按照书中的描述进行设置。 举个简单的例子, items表只有一个字段title要翻译,我想用五种语言保存title 。 所以我做了一个这样的表格(在add视图模板中): echo $this->Form->create($item, ['controller' => 'Items', 'action' => 'add']); echo $this->Form->input('title', ['label' => __('English')]); echo $this->Form->input('_translations.es.title', ['label' => __('Spanish')]); echo $this->Form->input('_translations.fr.title', ['label' => __('French')]); echo $this->Form->input('_translations.de.title', ['label' => __('German')]); echo $this->Form->input('_translations

2021-11-10 12:41:53    分类:技术分享    validation   cakephp   internationalization   multilingual   cakephp-3.1

如何只选择那些至少有一个关联记录的记录?(How to select only those records that have at least one associated record?)

问题 我有以下关联: Orders属于Sites 我执行以下查询: $sites = $this->Sites->find('all') ->select(['id', 'name']) ->contain([ 'Users' => function ($q) { return $q->select(['id', 'owner_id', 'firstname', 'lastname']); }, 'Orders' => function ($q) { return $q->where(['status >' => 0]); }, 'Orders.Agplans' ]) ->matching('Users', function ($q) use($owner_id) { return $q->where([ 'Users.owner_id' => $owner_id ]); }) ->all(); 但是大多数网站都没有订单,所以我得到了某种结果: (int) 100 => object(App\Model\Entity\Site) { 'id' => (int) 7966, 'name' => 'Site example', 'orders' => [], 'users' => [ (int) 0 => object(App\Model\Entity\User) {

2021-11-10 04:07:34    分类:技术分享    cakephp   query-builder   cakephp-3.1

Cakephp 3 回调,所有模型的行为(Cakephp 3 callbacks, behaviors for all models)

问题 我刚开始阅读 cakephp 3 文档(我已经使用 cake 2.x 开发了一段时间)并且想将一些网站从 2.x 迁移到 3。在我的AppModel cake 2 中,我有一些回调,特别是beforeFind和beforeSave ,包含一些关于数据库中几乎所有表的逻辑。 现在在 cake 3 中没有AppModel ,我如何完成同样的事情? 我能想到的最好的方法是将该代码放在某些行为的回调中,但我有 30 个模型,我应该在所有模型中一一加载行为吗? 谢谢 回答1 您还可以在src/Model/Table 文件夹中创建一个 AppTable: namespace App\Model\Table; use Cake\ORM\Table; class AppTable extends Table { public function initialize(array $config) { parent::initialize($config); $this->addBehavior('myBehavior'); $this->addBehavior('myBehavior2'); $this->addBehavior('myBehavior3'); } } 然后通过AppTable扩展您的 Table 类: namespace App\Model\Table; use App

2021-11-09 20:07:54    分类:技术分享    cakephp   cakephp-3.0   cakephp-3.1

CakePHP 3.1: Validation for translate behaviour fields (i18n)

I'm trying to add an item including multiple translations in one form with the CakePHP translate behaviour. How can I validate the translation fields? E.g. make specific languages required? Let's assume you have a simple items table with a separate translations table items_i18n, set up as described in the book. As simple example the items table has only one field title to translate and I want to save the title in five languages. So I make a form like this (in add view template): echo $this->Form->create($item, ['controller' => 'Items', 'action' => 'add']); echo $this->Form->input('title', [

2021-11-08 11:40:21    分类:问答    validation   cakephp   internationalization   multilingual   cakephp-3.1

Cakephp 3 callbacks, behaviors for all models

I just started reading cakephp 3 docs (I have been developing with cake 2.x for some time) and want to migrate some website from 2.x to 3. In cake 2 in my AppModel I have some callbacks, particularly beforeFind and beforeSave, that contain some logic concerning almost all tables in a database. Now in cake 3 there is no AppModel, how do I get the same thing done ? The best I can think of is to put that code in some behavior's callbacks, but I have like 30 models, should I load the behavior in all models one by one ? Thanks

2021-11-07 00:23:52    分类:问答    cakephp   cakephp-3.0   cakephp-3.1