CakePHP 2.0.3 Fatal Error flash() on object - cakephp

I've get this message when I tried to use a wrong controller and I figured it out that I'm not getting the right Error from cakephp I've got in 2.0.0 the right one:
Now when I try a wrong controller I get only this message:
Fatal error: Call to a member function Flash() on a non-object in
/srv/www/htdocs/web843/HTML/schaetzmal/lib/Cake/View/Layouts/default.ctp
on line 44
Does cakephp 2.0.3 have an bug or do I miss something to install to let work this or something else what I can do?

Make sure you've added the Session helper to your public $helpers array.
class SomethingsController extends AppController {
public $helpers = array('Session');
}
Or you could add it to a global AppController so that the Session helper is available to all controllers.
class AppController extends Controller {
public $helpers = array('Session');
}

I found some problems why my AppController didn´t work.
Like mensch says i have to use Session in my AppController for global but that´s not the solution because the book of cakephp says in "a global AppController"
NOTE
CakePHP merges the following variables from the AppController to your application’s controllers:
$components
$helpers
$uses
but it´s not happening. Because i overwrite it in the public variable $helpers.
therfore i take the parent given one and merge it with it:
<?
class AppController extends Controller {
public $viewClass = 'Theme';
public $theme;
public function beforeFilter() {
parent::beforeFilter();
$this->theme = 'SM';
$this->helpers = array('Form','Html','Js');
}
}
?>
the $this->helpers = array('Form','Html','Js'); do the merge and it works fine.
thank you guys
thanks for helping mensch that was the hack i needed

Related

Admin Panel/Interface in a CakePHP project

I want to create an "Admin Panel/Interface" in one of my CakePHP projects. You know its very common in modern web sites. At first, I planned to create a plugin for this, and tried to do it. It didn't work, no idea why, I'll ask for help for it later. But, next I saw that CakePHP already provides this feature, using "Scafolding". I am now trying this, but don't know why its not working as I expected. Here is what I did :
app/config/core.php :
---------------------
.
.
.
Configure::write('Routing.prefixes',array('admin'));
.
.
.
app/Controller/AppController.php :
----------------------------------
.
.
.
public $components=array(
'Session',
'Auth'=>array(
'loginRedirect'=>array('admin'=>true,'controller'=>'home','action'=>'index'),
'logoutRedirect'=>array('controller'=>'home','action'=>'index'),
'authorize'=>array('Controller')
)
);
.
.
.
I thought, there should be a seperate controller for Admin Panel, that's why I created it :
app/Controller/AdminsController.php :
-------------------------------------
<?php
App::uses('AppController','Controller');
class AdminsController extends AppController{
public $name='Admins';
public $scaffold='admin';
}
But it didn't work. So, I thought CakePHP provided this feature for all individual controller; I mean, I thought I am supposed to have the Admin Panel for all individual controller, not as a different module/controller/sub-system. So, I changed a little one of my existing controllers "Controllers1" :
<?php
App::uses('AppController','Controller');
class Controllers1Controller extends AppController{
public $name='Controllers1';
public $scaffold='admin';
}
then tried to go to this URL : my_site/admin/jobs/view
but still same result.
Please give me a suggestion, what should I do ? Should I create a new plugin for the "Admin Panel", or Scafolding is better ? And what is my fault ?
Thanks
The AdminsController is not necessary to use the admin prefix, all you need to do is define the Routing.Prefixes like you already did.
Configure::write('Routing.prefixes',array('admin'));
For the JobsController example that you mentioned to us what all you need to do to make it work is:
<?php
App::uses('AppController', 'Controller');
class JobsController extends AppController {
public $scaffold = 'admin';
}
Because the way to make Routing Prefixes work is to declare methods with the prefixes, not use an additional controller:
<?php
App::uses('AppController', 'Controller');
class ArticlesController extends AppController {
function admin_index() {
//This method can be found under /admin/articles
}
}

CakePHP returning missing database table

I tried all the suggested solution for this problem which is clearing your cache folders. I also disable the cache Configure::write('Cache.disable', true); My debug level is set to 2. But I always get a same error. The table that is missing is only new added table. By the way, I'm only running in my localhost.
this is my model named Department.php
<?php
class Department extends AppModel {
public $name = 'Department';
}
?>
this is my controller name DepartmentsController.php
<?php
class DepartmentsController extends AppController {
public $name = 'Departments';
public $helpers = array('Html', 'Form','Session');
public $components = array('RequestHandler','Session');
function index() {
$this->Department->recursive = 0;
$this->set('departments', $this->paginate());
}
}
?>
Advance thank you for the answer! :D
Assuming you are actually using a database I would try the following things:
If your database table isn't named using the cakephp conventions, manually set it:
public $useTable = 'table_name';
Make sure your database config is using the right database in app/config/database.php
If that model is in a plugin, use the plugin syntax when including it in the controller:
public $uses=array('PluginName.Department');

CakePHP 2.0 can't access Model from its own Controller

I'm still trying to migrate from CakePHP 1.3 to 2.0
I have a Controller UsersController and its Model User.
The class User has some constants which I could easily access from the UsersController using User::constant. But for CakePHP 2.0 it doesn't work: I get an error saying the User class is not found. It works if I App::Import('Model', 'User');.
It sure has to do with their built-in lazy loading in 2.0!
you simple need to tell this file that it has other dependencies
do that at the very top of your UserController file:
<?php
App::uses('User', 'Model');
then everything works fine
You can try setting the controller name (in UsersController):
var $name = 'Users';
Or using the "uses" var (in other controllers:
public $uses = array('User');
Does that not work?
Did you declare the name of the model like that ?
class User extends AppModel {
public $name = 'User';
}
Do you have others variables in your UsersController ?
I set variables in the model like this:
//Person model
public $genders = array('m' => 'male', 'f' => 'female');
Then get them from the controller like this:
//People controller
$genders = $this->Person->genders;
No special code needed. (Is this what you're talking about?)
Also, I don't believe you need to set the $name variable anywhere anymore - I think that was just for PHP 4. (not 100% on that part, but... I don't ever set that anymore, and my Cake2 apps run fine)
You have to load the model like this:
$this->loadModel('User');

Cakephp missing helper file error

I'm getting below error...I'm not sure what this means as I have included helper file in the view file...
Missing Helper File
Error: The helper file track/views/helpers/request_handler.php can not be found or does not exist.
Error: Create the class below in file: track/views/helpers/request_handler.php
<?php
class RequestHandlerHelper extends AppHelper {
}
?>
If you can let me know what this means that would be appreciated!
Thank you.
Jae
Unless you customized how your CakePHP works, this should apply to most cases:
Checklist
Make sure the helper file is created in /app/views/helpers/request_handler.php
Make sure the content of the request_handler.php looks like this:
class RequestHandlerHelper extends AppHelper {
var $name = 'RequestHandler';
//bla....
}
Make sure in the controller that renders the view has the helper array included
class FancyController extends AppController {
var $name = 'Fancy';
var $helpers = array('RequestHandler');
//bla....
}
I think that's all :)
Cheers
you have to include the helpers in the controller (app_controller if you want the helper to be available for views of all controller)
var $helpers = array('Form', 'Html', 'YourHelper');
If you are using any version of CakePHP, just open the file core.php in the folder config and edit the line Configure::write('debug', 0); to Configure::write('debug', 2);
It will say the erro and how to create the file and where to put it.

How to add helper or component on-the-fly with the controller action method

i don't want to add it as below cause i needed them only once in certain action method
(so do not useless load the memory)
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Html', 'Session');
var $components = array('Session', 'Email');
class UsersController extends AppController {
public function method_name() {
$this->helpers[] = 'MyHelper'
}
}
More on this in the documentation.
Hope that helps.
You can load helpers using
$this->helpers[] = 'MyHelper';
as Rob mentioned above, but this won't work for controllers because they have their initialize and startup methods that need to be called in order for them to work.
I've come across a bit of code on the web for loading components inside of a controller action: ComponentLoaderComponent
Yes, it is a component but it isn't very big so it shouldn't be a problem to include it in your controllers.
Either that or you can just study it to see how the component loading works and then write your own controller action to do the same.
I use a component for adding helpers and components on the fly:
$this->Common->addHelper('Tools.Datetime');
$this->Common->addHelper(array('Text', 'Number', ...));
$this->Common->addComponent('RequestHandler');
$this->Common->addLib(array('MarkupLib'=>array('type'=>'php'), ...));
etc
The complete code to this can be seen in the cakephp enhancement ticket I just opened:
http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/1277
Or with php markup:
http://www.dereuromark.de/2010/11/10/loading-classes-on-the-fly/
It also fixes some minor problems with the solution posted by mtnorthrop.
Plugins as well as passed options are now possible. Have fun.

Resources