klein/klein.php

How to get the method used in the controller?

Opened this issue · 1 comments

Hi, I have a question about your great router ...
I'm implementing it in my project and I had a question ... From my controller how can i Know which method was used? Post, Get, Patch or Delete?
Example :
In my routes.php file I have :
$klein->respond(['GET', 'PATCH'], '/posts/create', ['PostsController', 'create']);
So how in my controller I can know if the function was called in GET or in PATCH?
public static function create($req, $res, $serv, $app)
{
if (PATCH) {
//save to bdd
} else {
$params = [
'datas'=> [
'pageTitle' => 'Créer un nouveau Post'
],
];
echo $app->twig->render('Posts/create.html', $params);
}
}
Thanks you

edit :
Sorry, I know that in pure php you can use $_SERVER['REQUEST_METHOD'] to know the method used, but maybe in your Klein router you provide it?

According to the API docs, you can retrieve the Request Method via $request->method()!