2

I am using cakephp v3 and want to ask how to use custom helper in controller

I have tried below code.

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');
    }

and getting below error.

Error: Call to a member function decrypt() on boolean File D:\xampp\htdocs\myproject\src\Controller\PagesController.php Line: 204

I tried with some other methods like

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

Error: Class 'App\Controller\SecurityHelper' not found File D:\xampp\htdocs\myproject\src\Controller\PagesController.php Line: 204

7
  • ...and what happened? Commented Dec 2, 2016 at 10:50
  • check the updated question. Commented Dec 2, 2016 at 10:52
  • 3
    Helpers are for views, you do not use them anywhere else! Commented Dec 2, 2016 at 11:21
  • The answer is don't! Helpers are for views only as @ndm stated. As it's a custom helper you probably want to look at re-writing your code so that the method is available from within your controller as well as the helper. Commented Dec 2, 2016 at 12:25
  • If you only need your custom functionality in your controllers and not in your views, then a component would be the way to go. Commented Dec 3, 2016 at 0:18

1 Answer 1

1
$customdtfhelper = new \App\View\Helper\CustomDtfHelper(new \Cake\View\View());
$customdtfhelper->method_name();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.