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

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.

Related

Cakephp - MissingControllerException Controller class Robots.txt

My error log is loaded with all of the missing errors, for robots.txt, .css files, .png files and more. The site works, but getting errors. How do I get rid of these errors? I feel like I'm missing something simple, maybe a route or something?
2016-05-07 16:11:20 Error: [MissingControllerException] Controller class Robots.txtController could not be found.
#0 /app/webroot/index.php(93): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#1 {main}
2016-05-07 16:32:50 Error: [MissingControllerException] Controller class CssController could not be found.
#0 /app/webroot/index.php(93): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#1 {main}
If you try to fetch a resource that doesnt not exist, cake will try to find a controller by that name. So if you try to access /css/whatever and you have no css folder, cake will try to find a CssController to take you to /:controller/:action

Adding 403 Exception view to CakePHP application

I am trying to throw a 403 (Forbidden error) in my controller. When the exception is thrown, I would like to display the associated view. I was under the assumption that if the error is one of the built in exceptions, this would be pretty easy. I throw the exception like this:
throw new ForbiddenException("You do not have permission to view this page.");
I also created a view called "error403.ctp" in the app/view/Errors folder (it already contained 400 and 500). The problem is that when the error is thrown, it displays the error400.ctp view instead. Do I have to create custom exceptions for a built in error? What am I doing wrong.
From http://book.cakephp.org/2.0/en/development/exceptions.html#exception-renderer
"For all 4xx and 5xx errors the view files error400.ctp and error500.ctp are used respectively."
So you aren't doing anything wrong, that's just the default CakePHP behavior. As to how to change it so that you can have a 403 page separate from the other 4XX errors, see CakePHP 2.0 - How to make custom error pages?
I don't have the exact cake version you are using, so I'll use links to docs of version 2.3, but it should apply to any version 2.x.
If you look at the default ExceptionRenderer construct, you get this from the description
Creates the controller to perform rendering on the error response. If the error is a CakeException it will be converted to either a 400 or a 500 code error depending on the code used to construct the error.
And clear enough, from the code of that function, all errors get mapped to that:
$method = 'error500';
if ($code >= 400 && $code < 500) {
$method = 'error400';
}
You'll have to create a custom Exception Renderer if you want to use other views. Also, keep in mind that when using debug < 1, you'll only get 500 error pages
Captures and handles all unhandled exceptions. Displays helpful framework errors when debug > 1. When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown and it is a type that ExceptionHandler does not know about it will be treated as a 500 error.

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.1 new install Missing Controller Error

I am completely new to CakePHP and installed 2.1. I am getting this error:
Missing Controller
Error: Index.phpController could not be found.
Error: Create the class Index.phpController below in file: app\Controller\Index.phpController.php
<?php
class Index.phpController extends AppController {
}
Notice: If you want to customize this error message, create app\View\Errors\missing_controller.ctp
Stack Trace
APP\webroot\index.php line 96 → Dispatcher->dispatch(CakeRequest, CakeResponse)
ROOT\index.php line 40 → require(string)
I followed their guide at http://book.cakephp.org/2.0/en/installation/advanced-installation.html and tried everything it stated:
I enabled mod_rewrites (they were already enabled from something else)
I have all the .htaccess files in the directories
I have cake installed under my document root so I access it at localhost/cakephp/index.php
I do not know where to proceed from here. Thanks for any help you can give me.
Update: I just re-read your question and realized you're loading http://localhost/cakephp/index.php. Don't do that. Since you appended "index.php", it is trying to load a controller called "index.php" and the action "index" for that controller. Resulting mapped path to the "index.php controller" is app\Controller\Index.phpController.php.
Since you have the rewrites enabled, browse to http://localhost/cakephp without appending any filename.
Original answer:
Assuming you're using Apache, double-check the .htaccess in your /app/webroot directory. It should include the following:
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
Based on your error, it doesn't look like it's properly appending the path after your index.php file.
If you are using cakephp 2.0 or greater than rename the controller file name as the class name.
ie TaskController.php
class TasksController extends AppController {
--Your code inside class
}
I hope this will help you
the easiest solution is to stick to the "live environment" as close as possible.
this means using vhosts to use a "domain" and correctly root down to your webroot dir:
http://www.dereuromark.de/2011/05/29/working-with-domains-locally/
this leaves almost no room for error and also helps with other potential problems like "absolutely linked asset files" etc

cakephp app error problem

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/

Resources