【问题标题】:use custom helper in controller cakephp 3在控制器 cakephp 3 中使用自定义助手
【发布时间】:2016-12-02 10:48:20
【问题描述】:

我在用cakephp v3,想问一下controller中的custom helper怎么用

我试过下面的代码。

namespace App\Controller;

use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Request;
use Cake\ORM\TableRegistry;
use Cake\Auth\DefaultPasswordHasher;
use Cake\View\View;
use Cake\View\Helper\HtmlHelper;
use Cake\View\Helper\UrlHelper;
use Cake\View\Helper;
use App\View\Helper\SecurityHelper;

/**
 * Static content controller
 *
 * This controller will render views from Template/Pages/
 *
 * @link http://book.cakephp.org/3.0/en/controllers/pages-controller.html
 */
class PagesController extends AppController {
 function beforeFilter(Event $event) {
        parent::beforeFilter($event);

        $this->viewBuilder()->helpers(['Security']);
}

 public function receiveSaving() {
        pr(($this->request->params['id'])); //die;
        pr($this->Security->decrypt('7JCdO3vIqAU_EQUALS_')); die;
        $this->viewBuilder()->layout('front');
        $this->set('title', '');
        $this->set('meta_title', '');
        $this->set('meta_description', '');
        $this->set('meta_keywords', '');
        //$this->render('savings_bond_result');
    }

并低于错误。

错误:在布尔文件上调用成员函数解密() D:\xampp\htdocs\myproject\src\Controller\PagesController.php 行:204

我尝试了其他一些方法,例如

$Security = new SecurityHelper(new \Cake\View\View());
pr($Security->decrypt('7JCdO3vIqAU_EQUALS_')); die;

错误:找不到类“App\Controller\SecurityHelper”文件 D:\xampp\htdocs\myproject\src\Controller\PagesController.php 行:204

【问题讨论】:

  • ...发生了什么?
  • 检查更新的问题。
  • 助手用于查看,您不要在其他任何地方使用它们!
  • 答案是不要!助手仅用于查看@ndm 所述。由于它是一个自定义帮助程序,您可能希望重新编写代码,以便该方法在您的控制器和帮助程序中都可用。
  • 如果您在控制器中而不是在视图中需要自定义功能,那么组件将是您的最佳选择。

标签: php cakephp model-view-controller cakephp-3.0


【解决方案1】:
$customdtfhelper = new \App\View\Helper\CustomDtfHelper(new \Cake\View\View());
$customdtfhelper->method_name();

【讨论】:

    猜你喜欢
    • 2015-05-31
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2012-08-08
    • 2012-05-19
    • 1970-01-01
    相关资源
    最近更新 更多