klein / klein.php

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

How to get the method used in the controller? #378

Open trenyture opened 6 years ago

trenyture commented 6 years ago

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?

MartinSerdar commented 6 years ago

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