cakephp app error problem - cakephp

I have the following function in my app_controller:
function beforeFilter() {
$this->set('lastThreePosts', $this->Blog->find_latest_posts());
}
I then use the variable 'lastThreePosts' that I have set in my layout. This works fine apart from on my 404 page where I get the following error:
Notice (8): Undefined index: lastThreePosts [APP\views\layouts\default.ctp, line 29]
I have made my own 404 page by created the file error404.ctp in my /views/errors/ directory.
So it seems when theres a 404 error the code in the controller doesn't get executed. Does anyone know how I can make it work?
Thanks

Phew! I found the answer here:
http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/

Related

CakePHP Sending Files

I am trying to render images/pdfs that exist outside of my webroot. So this is what I'm doing in my controller to Send the File - as described here. But I'm getting a Fatal Error. Any ideas why and how to fix this? I'm running CakePHP 2.4.5.
$this->autoRender = false;
$path = '/path/to/valid/file.pdf';
$this->response->file($path);
return $this->response;
Fatal error: Call to undefined method Folder::slashTerm()
Stack trace:
{main}() /Path/To/My/htdocs/mysite/app/webroot/index.php:0
Dispatcher->dispatch() /Path/To/My/htdocs/mysite/app/webroot/index.php:96
Dispatcher->_invoke() /Path/To/My/htdocs/mysite/lib/Cake/Routing/Dispatcher.php:160
Controller->invokeAction() /Path/To/My/htdocs/mysite/lib/Cake/Routing/Dispatcher.php:185
ReflectionMethod->invokeArgs() /Path/To/My/htdocs/mysite/lib/Cake/Controller/Controller.php:490
PagesController->load_media() /Path/To/My/htdocs/mysite/lib/Cake/Controller/Controller.php:490
CakeResponse->file() /Path/To/My/htdocs/mysite/app/Controller/PagesController.php:227
File->__construct() /Path/To/My/htdocs/mysite/lib/Cake/Network/CakeResponse.php:1265
File->pwd() /Path/To/My/htdocs/mysite/lib/Cake/Utility/File.php:91
Problem solved. I had a Plugin that had a class Folder that was taking this over.

CakePHP 2 MissingControllerException in error.log => show accessed url

Is it possible to create/extend a CakePHP Exception handler which shows the url the user tried to access which throws the exception?
Usually when I look into my error.log file I see something like this:
2012-08-01 13:39:20 Error: [MissingControllerException] Controller class UserController could not be found.
#0 /www/htdocs/w0081e0e/app/webroot/index.php(104): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#1 {main}
I have a working UsersController but I'm not sure which url the user tried to access which in return throw this error. It would help me to see the url in the error.log file. Is that possible? Would I have to create/extend an exception handling file?
Update:
Instead of writing my own ExceptionHandler I just updated the "handleException" function in "lib/Cake/Error/ErrorHandler.php":
if (!empty($config['log'])) {
$message = sprintf("[%s] %s\n%s",
get_class($exception),
$exception->getMessage(),
$exception->getTraceAsString().' - http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n"
);
CakeLog::write(LOG_ERR, $message);
}
I know that this is overwritten once I update my CakePHP Version but its a quick fix that works really fine for my situation.
You can write your own exception handlers. It's pretty well documented at http://book.cakephp.org/2.0/en/development/exceptions.html#create-your-own-exception-handler-with-exception-handler
Anyway the URL seems to be like http://something/user/something. The error says UserController could not be found, which is true as you only have a UsersController (plural). If you just want to avoid this error it might be enough to find the file which creates a /user/ URL.

mpdf no output and php errors in mpdf.php

I'm trying to generate a pdp file in a cakephp application. Therefore, I use the mpdf library as a vendor. But when I try to make a even very simple output it doesn't work. Then when I use the debug property, it shows php errors in the mpdf.php file.
Here is my source code:
<?php
$mpdf=new mPDF();
$mpdf->WriteHTML('hello');
$mpdf->debug = true;
$mpdf->Output();
exit;
?>
And these are the errors shown in the browser:
Notice (8): Undefined index: BODY [APP\vendors\MPDF54\mpdf.php, line 14242]
Notice (8): Undefined index: BODY>>ID>> [APP\vendors\MPDF54\mpdf.php, line 14288]
Notice (8): Undefined offset: -1 [APP\vendors\MPDF54\mpdf.php, line 14421]
Thank you for your help!
This is not a CakePHP problem but related to the library you're using.
Read about how to use the WriteHTML() method.
http://mpdf1.com/manual/index.php?tid=121
And try passing 2 as the 2nd argument.
$mpdf->WriteHTML('hello', 2);
If this still does not work read the documentation, check the examples there.
This is caused by buggy mpdf code. It depends on error (level "notice") reporting to be switched off (it switches it off itself). But if you handle errors some nonstandard way, it is problem..
I solved it by ignoring errors from mpdf.php file in my custom error handler.
I was using my custom error reporting via set_error_handler();

I am getting error : Fatal error: Call to undefined method Debugger::checkSessionKey() in F:\xampp\htdocs\cakecms\views\pages\home.ctp on line 3

Fatal error: Call to undefined method Debugger::checkSessionKey() in F:\xampp\htdocs\cakecms\views\pages\home.ctp on line 3
in cake php .
Are you trying to call $this->Session from your home.ctp? I don't think $this->Session is available to views.
If you show your code for home.ctp we could better see what is going on.

CakePHP "Warning: Cache not configured properly"

I am using CakePHP and getting the following error
Warning: Cache not configured properly. Please check Cache::config(); in APP/config/core.php in D:\PHP-SERVER\cheesecake\cake\libs\configure.php on line 663
Notice: Undefined variable: Route in D:\PHP-SERVER\cheesecake\app\config\routes.php on line 38
Fatal error: Call to a member function connect() on a non-object in D:\PHP-SERVER\cheesecake\app\config\routes.php on line 38
Any problem in the settings?
Which CakePHP version are you using?
The fatal error looks like a typo. Open the file D:\PHP-SERVER\cheesecake\app\config\routes.php and check what there is written on line 38. It looks like there is a letter missing.
It should be something along the lines of
Router::connect(...[whatever your route settings are]...);
and I guess in your file it says (note the missing r in Router)
Route::connect(...);
Did you do what the other error message said? Check the Cache::config call in app/config/core.php. You seem to have not properly set your cache configuration.
The default call looks like
Cache::config('default', array('engine' => 'File'));
If you changed that post the code-piece to figure out if something is wrong..

Resources