klein / klein.php

A fast & flexible router
MIT License
2.66k stars 290 forks source link

Klein dispatch() LockedResponseException #395

Closed karamusluk closed 6 years ago

karamusluk commented 6 years ago

Hello everyone, I have a problem in my index.php file and following error is produced anytime once i go to the blabla.com/ . I haven't tested all the pages in the system but i guess i have to figure the problem in the root.

Following is the error message generated by debugger.

PHP Fatal error: Uncaught exception 'Klein\Exceptions\LockedResponseException' with message 'Response is locked' in /home/public_html/host/inc/phplib/klein.php:305 Stack trace: /home/public_html/host/inc/phplib/klein.php(240): Klein\AbstractResponse-requireUnlocked() 1- /home/public_html/host/inc/phplib/klein.php(1920): Klein\AbstractResponse->code(500) 2- /home/public_html/host/inc/phplib/klein.php(1625): Klein\Klein -error(Object(Klein\Exceptions\LockedResponseException)) 3- /home/public_html/host/index.php(391): Klein\Klein->dispatch() 4-{main} thrown in /home/public_html/host/inc/phplib/klein.php on line 305

And below is my root routing code.

$klein->respond('GET', '/', function ($request, $response, $service,$app) { if (!isset($_SESSION['d]) ){ $response->redirect('n')->send(); } if(isset($_SESSION['key'])){ $response->body(''); } else{ $index = include('views/e.php'); $response->body($index); } });

Can anyone help me to figure the problem out?

Thak you for your all contributions in advance.

eimajenthat commented 6 years ago

I think after $response->redirect('/auth/login')->send(); you need something to stop execution of the rest of your logic. You could do a return or a die().

The send finishes processing your response and locks it, but there's no way for your function to know that, so it keeps on executing. Then it tries to put something in the $response->body() and it can't, because the response is locked.

karamusluk commented 6 years ago

Thank u very much for your response :) that worked well for me. Sorry for the late response