today I have a question about Klein and I am asking becasue there isn't much documentation on this library on the internet. I want to redirect the user back to /login if they are on any page other than "/login" or "/register" AND they arent logged i, below I have tried:
$this->klein->respond('GET', '!@^/login|register', function ($request, $response, $service, $app) {
if (!Latrina::getLibrary('latrina.user.user')->loggedIn()) {
$response->redirect('/login')->send();
}
});
It works, but it redirects to /login if they are on /register which is pretty annoying because it disables them from signing up..
I haven't personally verified that the $request param to this callback is a string -- it may be an object or array, with the URL you need to check as a property. But this general logic is what you're looking for:
$this->klein->respond('GET', '!@^/login|register', function ($request, $response, $service, $app) {
if (!Latrina::getLibrary('latrina.user.user')->loggedIn()) {
if (strpos($request, 'login') !== false) {
$response->redirect('/login')->send();
} else {
$response->redirect('/register')->send();
}
}
});