PHPExcel works in one view but thows errors in all others - cakephp-2.0

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") );

Related

In CakePHP 2, how do you tell the Paginator component which model to use?

Given the following working example,
// ProductsController.php
<?php
App::uses('AppController', 'Controller');
class ProductsController extends AppController {
public $helpers = array('Html', 'Form');
public $components = array('Session', 'Paginator');
public $paginate = array(
'limit' => 5
);
public function index() {
$this->Product->recursive = -1;
$this->set('products', $this->paginate());
}
}
?>
What tells Paginator which model to use when it sets the variable? Currently this seems to be automatically using the Products model, but I don't really understand why. Is it just part CakePHP's magic that it selects the model that has the same name as the current controller? And if so, how would I tell Paginator to use some other model? Like if I wanted to also paginate the User model on the same page, how would I implement that?
According to Cakephp2,
$this->paginate() by default works on the current model.
if you want to use other model on the same page you can do like this:
$this->paginate('User');
You can all pass other parameters like:
$this->Paginator->settings = array(
'fields' => array('User.*'),
'order' => array('User.username' => 'asc'),
'limit' => 10,
);
$this->set('users', $this->paginate('User'));
Reference: Pagination
So, after some trial and error, it seems like by default Paginator will just use whatever model is associated with the controller via the naming conventions (the 'User' model is associated with the 'UsersController' controller, and so on).
The first argument of $this->paginate() will accept a different model if you want to use one, for example $this->paginate('Dinglehopper'), but the specified model needs to be available to the controller/action in order for it to work. In order to do that you need $this->loadModel('Dinglehopper'); inside the action where $this->paginate('Dinglehopper') is called.
So in the hypothetical situation where you want to use and paginate the model 'Dinglehopper' inside your 'Products' controller you would do,
// ProductsController.php
<?php
App::uses('AppController', 'Controller');
class ProductsController extends AppController {
public $helpers = array('Html', 'Form');
public $components = array('Session', 'Paginator');
public $paginate = array(
'limit' => 5
);
public function index() {
$this->loadModel('Dinglehopper');
$this->Product->recursive = -1;
$this->set('dinglehoppers', $this->paginate('Dinglehopper'));
}
}
?>
$this->loadModel('Dinglehopper'); makes the model 'Dinglehopper' available to the action, and then $this->paginate('Dinglehopper') returns the paginated Dinglehopper model.

Cakephp3.0 I am calling Postcategories controller into Appcontroller and it is error " Call to undefined method Cake\Core\App::import() "

My Code tries to fetch all Main categories of the posts into Appcontroller to show on the homepage:
namespace App\Controller;
use Cake\Core\App;
use Cake\Controller\Controller;
class AppController extends Controller
{
public $helpers = ['Html', 'Form', 'Session','Time','Tree'];
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->maincategories();
}
function maincategories(){
App::import('Controller','Postcategories');
$postcates = new PostcategoriesController;
$postcates = $postcategory->find('threaded');
}
}
Your maincategories() method is wrong. You need the model, not the controller to retrieve the data from. You need to use TableRegistry::get('Postcategories') to get the Postcategories model and then call the find on that:-
public function maincategories()
{
$Postcategories = TableRegistry::get('Postcategories');
$this->set('postcategories', $Postcategories->find('threaded'));
}
$this->set() is setting the categories as a view variable ($postcategories). You will need to make sure you include use Cake\ORM\TableRegistry; at the top of your AppController file.
Make sure you've fully read the docs on retrieving data.

missing method public function index()

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

Cakephp 1.3 helper not found in default.ctp

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

Cakephp country helper

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

Resources