Error handling in CakePHP 2.0. Taken viewVars

I had a strange problem with errors in Cake 2 that I never had in version 1.3.

When a certain exception occurs (i.e. NotFoundException), Cake error processing begins. By default, it launches CakeErrorController and does all this (see source ) and .. escapes viewVars. But I have some points of view set in my AppController beforeRender, and I do not want them to be escaped because they contain html and they should be displayed in the layout. What is the reason for this behavior? How can I tell Cake not to do this?

Thanks.

Update:

Well. I developed some solution:

I created Controller / AppErrorController.php :

App::uses('CakeErrorController', 'Controller'); class AppErrorController extends CakeErrorController { public function beforeRender() { AppController::beforeRender(); } } 

and Lib / Error / AppExceptionRenderer.php :

 App::uses('ExceptionRenderer', 'Error'); class AppExceptionRenderer extends ExceptionRenderer { protected function _getController($exception) { App::uses('AppErrorController', 'Controller'); if (!$request = Router::getRequest(false)) { $request = new CakeRequest(); } $response = new CakeResponse(array('charset' => Configure::read('App.encoding'))); try { $controller = new AppErrorController($request, $response); } catch (Exception $e) { $controller = new Controller($request, $response); $controller->viewPath = 'Errors'; } return $controller; } } 

And in Config / core.php I installed:

 Configure::write('Exception', array( ..., 'renderer' => 'AppExceptionRenderer', ... )); 

It seems to work as expected, but is there an even more elegant solution?

Thanks.

+4
source share
1 answer

Probably also because CakeErrorController is used for all exceptions - so the environment can send any message for any error without worrying about HTML input or garbled output. I assume this is just a reliable conclusion, as Cake docs describe the controller as "[omitting] some regular callbacks to ensure that errors are always displayed . " This is a thin, trimmed controller for a specific purpose.

Unfortunately, this means that they hardcoded the CakeErrorController beforeRender method, so there is no way to disable it. The only simple options that I can think of are hacked ...

  • Create your own error view that uses the htmlspecialchars_decod() function to convert the escaped text back to unescaped text. (If you want to be thorough, you also check the encoding, etc. Basically change what h() did for your string)
  • Convert the message to an object (since beforeRender does not escape objects).

Fortunately, there is a proper workaround in the docs, and you already understood that.

It seems like a lot of work just to get the HTML in your view, because it's a lot of work. MVC is supposed to help share your problems, but you manage this separation by creating HTML in your controller, and not in your view.

0
source

Source: https://habr.com/ru/post/1387274/


All Articles