2

hope you can help me with this strange problem: I'm trying to redirect from within a Controller but Kohana keeps throwing an Exception that I just can't figure out why:

code in Cadastro.php:

try{
     $this->redirect('/dados', 302);
} catch (Exception $e) {
                $this->response->body(Json_View::factory(array("line ".$e->getLine()." of file ".$e->getFile().":".$e->getMessage()." - trace as string: ".$e->getTraceAsString())));
}            }

the stack trace message returned by the exception in the code above is:

#0 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\HTTP.php(33): Kohana_HTTP_Exception::factory(302)
#1 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Controller.php(127): Kohana_HTTP::redirect('\/dados', 302)
#2 C:\\xampp\\htdocs\\grademagica\\modules\\grademagica\\classes\\Controller\\Cadastro.php(123): Kohana_Controller::redirect('\/dados', 302)
#3 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Controller.php(84): Controller_Cadastro->action_signin()
#4 [internal function]: Kohana_Controller->execute()
#5 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Request\\Client\\Internal.php(97): ReflectionMethod->invoke(Object(Controller_Cadastro))
#6 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Request\\Client.php(114): Kohana_Request_Client_Internal->execute_request(Object(Request), Object(Response))
#7 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Request.php(990): Kohana_Request_Client->execute(Object(Request))
#8 C:\\xampp\\htdocs\\grademagica\\index.php(123): Kohana_Request->execute()
#9 {main}

Line 123 in Cadastro.php is "$this->redirect('/dados', 302);", as described above. Does anyone can help me showing what I'm doing wrong? I am following the exact directions of the documentation

Thanks

Andrade
  • 1,179
  • 12
  • 15

1 Answers1

5

Let's see what happens.

You call $this->redirect('/dados', 302);, so let's look at it's source code:

public static function redirect($uri = '', $code = 302)
{
    return HTTP::redirect($uri, $code);
}

Ok, we learnt $this->redirect('/dados') would suffice, lets look at HTTP::redirect() next:

public static function redirect($uri = '', $code = 302)
{
    $e = HTTP_Exception::factory($code);

    if ( ! $e instanceof HTTP_Exception_Redirect)
        throw new Kohana_Exception('Invalid redirect code \':code\'', array(
            ':code' => $code
        ));

    throw $e->location($uri);
}

It will create an exception (HTTP_Exception_$code) and then throw it.

The exception should bubble up to Request_Client_Internal::execute_request() where the following catch block should handle it:

catch (HTTP_Exception $e)
{
    // Get the response via the Exception
    $response = $e->get_response();
}

But since you catch the exception it won't bubble up. This is one way to fix it.

try{
     $this->redirect('/dados', 302);
} catch (HTTP_Exception_Redirect $e) {
    throw $e;
} catch (Exception $e) {
                $this->response->body(Json_View::factory(array("line ".$e->getLine()." of file ".$e->getFile().":".$e->getMessage()." - trace as string: ".$e->getTraceAsString())));
}
Darsstar
  • 1,885
  • 1
  • 14
  • 14
  • Thanks for your help Darsstar, but it didn't completely worked for me. I can see WHERE the exception is raised, as you pointed out, but I still can't understand WHY it happens. Nevertheless, what bothers me is that it still isn't redirecting. I've called the controller via Jquery's ajax in order to validate a form in the server side before redirecting. Now, with your help, the ajax call doesn't return the exception data anymore, it returns the SOURCE CODE of the page I want to redirect to. Do you have any idea why? – Andrade Oct 12 '13 at 21:08
  • 1
    As to WHY the exception get thrown, that is how Kohana devs decided they would do redirecting via the provided helper method. As to why you get SOURCE CODE, I have no idea... Unless you mean the [Kohana error page](http://kohanaframework.org/3.3/guide/kohana/errors#example). You could try $this->response->status(302)->headers('Location', '/dados'); and see if that is any better. (You might want to include the logic from [HTTP_Exception_Redirect::location()](http://kohanaframework.org/3.3/guide-api/HTTP_Exception_Redirect#location) too.) – Darsstar Oct 13 '13 at 07:45
  • That's because it's what `$this->request('/dados')` should achieve by throwing an instanace of HTTP_Exception_Redirect. But it would seem it never reaches Request_Client_Internal::execute_request() because it gets caught somewhere in between. Which could potentially indicate your controller is getting fat. Also `$this->response = Response::factory()->status(302)->headers('Location', '/dados');` is slightly cleaner since you start with a clean slate as far as the Response object is concerned. – Darsstar Oct 14 '13 at 08:34
  • I think it isn't possible to redirect from within a controller when it is called via ajax. Apparently it recognizes the target (redirected) page as the return for the ajax function. So what I did is return a json to indicate that redirection is needed and the URL, to be processed by a client side javascript test to redirect from the client side. However, all the methods described by Darsstar work for when a controller is called synchronously (not via ajax). – Andrade Oct 24 '13 at 16:21