The controller
class PagesController extends AppController
{
public $helpers = array('Formatacao');
.
.
.
}
The helper
class FormatacaoHelper extends AppHelper
{
var $name = 'Formatacao';
.
.
.
}
I'm getting this error in layout:
Undefined property: View::$Formatacao [APP\views\layouts\default.ctp, line 51]
Trying to use like this: $this->Formatacao->get_clean_base_url(false);
What i'm missing?
Best regards.
You are not supposed to add the helpers to CakePHP's PagesController.
Do that in AppController instead:
class AppController extends Controller {
public $helpers = array('Formatacao');
}
Related
I have a view that generates an Excel sheet and it works fine. But now when I go to any other view within that model, I get an error:
Missing Helper
Error: PHPExcelHelper could not be found.
Error: Create the class PHPExcelHelper below in file:
app_myapp/View/Helper/PHPExcelHelper.php
<?php
class PHPExcelHelper extends AppHelper {
}
My controller:
App::import('Vendor', 'PHPExcel', array('file' => 'PHPExcel.php'));
class InvoicesController extends AppController {
public $components = array('RequestHandler','PhpExcel');
public $helpers = array('Html', 'Form', 'Js'=>array("Jquery") ,'PHPExcel' );
I tried putting the App::import line in the function that is generating the excel sheet but I still get the same error on any other page in the model.
Help/direction is much appreciated!
I fixed it by removing PHPExcel from the helpers line...changing:
public $helpers = array('Html', 'Form', 'Js'=>array("Jquery") ,'PHPExcel' );
To this:
public $helpers = array('Html', 'Form', 'Js'=>array("Jquery") );
I have queried on the User table inside the AppController as below
<?php
class AppController extends Controller {
public function beforeFilter() {
function beforeFilter() {
parent::beforeFilter();
if ($this->Session->read('Auth.User.id')) {
$userLoginInfo = $this->User->findByUserId($this->Session->read('Auth.User.id'));
$this->set('userLoginInfo', !empty($userLoginInfo) ? $userLoginInfo : NULL);
}
}
}
}
?>
The $userLoginInfo is available in all ctp files, but I want to access it in all other controller actions as well.
now you put this code in AppController
function beforeFilter(){
$this->set(‘accesstest’ , ‘abc’);
}
And We have to use it in Other controller file say anotherController.php
then we will use $this->viewVars.
here we will be used
$test = $this->viewVars[‘accesstest’];
$this->set('test',$test);
Your own answer is applicable if wanting to set a variable for all Views, but this was not what you were asking in your question.
If you are extending AppController correctly then you can create a property of the AppController class that would then be accessible from any controller that extends it:-
class AppController extends Controller {
public $accesstest = 'abc';
}
Then in any controller that extends AppController you can use:-
$test = $this->accesstest;
echo $test; // 'abc'
However, if you want to share a variable that you want accessible from all controllers that can be changed and you want the change remembering then use the Session:-
$this->Session->write('accesstest', 'abc');
$test = $this->Session->read('accesstest');
$userLoginInfo = $this->viewVars['userLoginInfo'];
Missing Method in BusinessController
Error: The action index is not defined in controller BusinessController
Error: Create BusinessController::index() in file: app/Controller/BusinessController.php.
If I add this method, it brings me to the homepage. I want to go business_index.ctp
Here is my controller
App::uses('Controller', 'Controller');
class BusinessController extends AppController {
public $name = 'Business';
public $components = array('Auth','RequestHandler','Cache');
public $uses = array('User','CreditHistory');
public function beforeFilter(){
parent::beforeFilter();
}
function Sync(){
$this->layout = $this->autoRender = false;
}
//Admin Dashboard
public function business_index(){
$this->set('title','Dashboard');
$uid = $this->Auth->User('id');
$this->set('user_id',$uid);
}
I have a view name business_index.ctp inside a folder name Business
Not sure what I am doing wrong.
You could either do redirect $ this-> redirect ('business_index'); in your index method or add router rule in config/routes.php. See http://book.cakephp.org/2.0/en/development/routing.html
Im trying to load one appmodel to many controllers. For example i have appmodel called Item and i want to get some items in HomeController and ContactController. When i execute this code:
class HomeController extends AppController {
public function index() {
$items = $this->Item->find('all');
$this->set('items', $items);
}
}
I got this error:
Call to a member function find() on a non-object
How can i get thing from database in many views in CakePHP?
you need to declare which model you want to use in your controller.
class HomesController extends AppController {
var $uses = array('Item', 'AnotherModel');
...
}
var $uses=array('Item');
If using this model in multiple controllers, put this in app controller.
I have a problem with this helper : https://github.com/kshakirov/cakephp-lang-helper
This helper give me this error and I don't find why :
Fatal Error
Error: Call to a member function input() on a non-object
File: \app\View\Helper\LangHelper.php
Line: 670
I am guessing the problem is that LangHelper overrides the parent __construct method, preventing Cake from setting up the Helper correctly. Change LangHelper's __construct() to the following:
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
$this->mapper = $this->parseLangHeaders();
$this->langCode = $this->findLangCode();
$this->countryCode = $this->findCountryCode();
}
Did you activate the FormHelper in the AppController?
App::uses('FormHelper', 'View/Helper'); // Don't forget this one in Cake 2.x
class AppController extends Controller
{
public
$helpers = array('Form');
}