Experiencing strange behaviour with cakephp - cakephp

I was setting up this application in cakephp.I have homes controller which works well and then i have chokates controller which has an index action.But whenever i run this chokates controller i get an error
Call to a member function charset() on a non-object
I had a print of $this in top of this controller which shows me that html and javascript helpers are loaded already.Then why i get this error i dont know.
Please help
Here is the link http://www.maninactionscript.com/chokate
go to the chocolates link.
Regards
Himanshu Sharma

Well, looking at your error message it appears that a couple things are happening here that should be fixed:
The view for ChokateController::index() was not found.
Ensure that you have an index.ctp file in /app/views/chokate/.
The HtmlHelper object likely isn't being added to the view because you have a manually configured list of $helpers. If you have assigned an array to $helpers in ChokateController ensure that Html is listed.

Related

Fatal error: Call to undefined method View::redirect() cakephp

I am developing an application in CakePHP 1.3. Everything is working fine but one error is there.
When I use redirect() in my .ctp page it gives error
Fatal error: Call to undefined method DebugView::redirect()
As redirect is working fine when I use it in controller.
I have included helpers as follows in my AppController
var $helpers = array('Html', 'Form','Session');
Please help
thanks in advance
You never redirect (or output any header if possible) in the view layer.
Use the controller to do so.
The view then should only render the output according to the desired output format (html, xml, json, ...). Header stuff is part of the reponse and responsibility of the controller (and in 2.x the response class itself).
So your observation that it will work with controllers and not inside views is correct.
"Call to undefined method" always is a good indicator for a method not being available in this scope.
Redirects being part of the "logic" makes them only available in controllers and components. Never ever in the view (output after all logic happened).
In your controller:
$this->redirect( 'url, absolute or relative here' );
Done.

Cakephp view not working

When I want to see for example localhost/pages/index, the localhost page is shown. The controller is working (the debug shows proper value defined in the controller). Also the title of the localhost/pages/index is set properly ("Posts"). Can somebody help me?
The page is in /app/View/webroot
Also, the view is working, when die() inserted in the end of index.ctp it is shown properly.
localhost/pages/index refers to a file called index.ctp in app/View/pages
app/View/webroot is for your assets - images, css, js and so on.

cakephp v2 layout not loading

Hi I want to add a new layout to my cakephp, but somehow the system keeps looking in the view folder from the controller instead of the /app/View/Layouts folder.
Error: The view for TestsController::desktop() was not found.
Error: Confirm you have created the file: /app/View/Tests/desktop.ctp
the desktop.ctp file is in /app/View/Layouts. The same place as the default.ctp
The code in the controller is:
public function desktop() {
$this->layout = 'desktop';
}
What is wrong here? I don't understand why cakephp keeps looking in the view/controller-name folder... I need this fixed because I want to use this layout for other controllers. Thanks.
If you read the message carefully, you'll see cake is telling you it cannot find your view, not your layout.
So, create an empty /app/View/Tests/desktop.ctp and see what happens. I'm hoping magic.. :)

How to access a Model that isn't part of a Controller in CakePHP

I am currently working in a controller called Content. In this controller, I have a function that will be called when someone goes on to the homepage, and most of the data will be pulled from the Content model.
However, I want to display data from another model called Phones. When I type this code:
$phones = $this->Phone->find('all');
I get the following error:
Fatal error: Call to a member function find() on a non-object
When I change the Phone part of the PHP code to Content it works fine. So I'm guessing that I can't, at the moment, access the Phone model from inside the Content controller.
Is there a way to achieve a way of accessing a model externally from a controller?
$this->loadModel('Phone'); # Important: singular!!!
$this->set('phones', $this->Phone->find('all'));
try with below
$this->loadModel('Phone');
$this->set('phones', $this->Phone->find('all'));
cheers

cakePHP: I need to include a helper for when there is an error

I have made a helper class called Navigation that gets used on every page because it does stuff to my main navigation menu. So in order for this to work I have included the helper in my pages controller like so:
var $helpers = array('Html', 'Javascript', 'Navigation');
however when there is an error like a missing view or something the helper can't be found and I get a reference to a non-object error that messes my page layout up. I'm guessing this is becuase an error page uses a different controller, however there isn't a file error_controller.php or anything in the controllers file. So my question is where do I need to declare the helper so it can be found by an error page. Would I need to make an error controller file or is there already a file that I can add it in to?
Any help would be much appreciated
Thanks
If it's used on every page, why not add it to the AppController?

Resources