The intended redirect does not occur when a 400 series error occurs.

Asked 2 years ago, Updated 2 years ago, 51 views

The intended redirect does not occur when the following code is executed (InputController), and
MMissing Method in UsersController
Error: The action index is not defined controller UsersController"
appears.
How do I redirect to the ErrorsController index action?
*The php version 5.4 is the environment of Cakephp2.

class InputController extensions AppController {
    public function index()
    {
        try{
          through new Exception();
        } catch(Exception$e){
          through new NotFoundException();
          return;
         }
     }
App::uses('ExceptionRenderer', 'Error');
classAppExceptionRenderer extensionsExceptionRenderer {

    public function error400($error){
        $this->controller->redirect(array('controller'=>'errors','action'=>'index'));
    }
}
class ErrorsController extensions AppController {

    public function index() {
    }
}

php cakephp

2022-09-30 16:22

2 Answers

Do you have AppExceptionRender enabled?

To enable AppExceptionRender, set the Exception.render option to app/Config/core.php.

app/Config/core.php

Configure::write('Exception', array(
        'handler' = > 'ErrorHandler::handleException',
        'render' = > 'AppExceptionRenderer', // < -- Rewrite here
        'log' = > true
    ));

Note:
Use your own renderer using Exception.render to handle application exceptions
https://book.cakephp.org/2.0/ja/development/exceptions.html#exception-renderer


2022-09-30 16:22

UThe action index is not defined in the controller called UsersController br
(That mind: I don't want to be told to do something that I didn't define.)
This is the content of the error, so there is a problem with the controller program called UsersController.
 However, the code written in the question does not include the UsersController program.

Advice
1) First of all, let's focus on the UsersController that the error message points out that the error is occurring.
 If you look elsewhere, you will waste your time.
 If you don't know how to fix it, I think it would be easier to add the UsersController code to the question and get an answer if you ask me what I'm having trouble with.

2) The index function of the InputController is the code that only raises the exception.
 What kind of purpose was it designed for?
 If you want to treat errors in the 400s as exceptions, I think it would be honest to run them in the try clause and supplement them with the catch clause to take action (redirect, etc.).


2022-09-30 16:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.