Frage

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

War es hilfreich?

Lösung

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())));
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top