Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I setup routes in Kohana to only match particular HTTP methods (GET/POST/etc)

Tags:

rest

php

kohana

I'm exploring a few PHP frameworks and the current front runner is Kohana.

Having a Rails background I've become used to what the rails community calls "RESTful" routes. So a "GET /posts" displays all posts and is handled by the index method of the Posts Controller. A "POST /posts" creates a new post object and is handled by a different method of the Posts Controller.

Since the path in both these 2 requests is identical, the router needs to make decisions based on the HTTP method.

Is the router in Kohana capable of doing this?

like image 602
James Healy Avatar asked Jul 15 '09 00:07

James Healy


2 Answers

Kohana does not support RESTful routes by default, but there is a RESTful module that adds support for it. See the RESTful wiki for usage.

Kohana v3.x supports RESTful controllers directly. Just extend Controller_REST instead of Controller and all the route action will be the request method. (A POST request would be targeted to action_post, etc.)

like image 105
shadowhand Avatar answered Oct 19 '22 18:10

shadowhand


You could also add these lines to your controller's before() method:

if ($this->request->method() == "POST")
{
  $this->request->action("post_".$this->request->action());
}

so GET /controller/posts will be handled by the action_posts() method in your controller, while POST /controller/posts will be handled by the action_post_posts() method.

PS: The built-in Controller_REST was removed in Kohana 3.2

like image 3
flammel Avatar answered Oct 19 '22 18:10

flammel