Skip to content

Instantly share code, notes, and snippets.

@Kindari
Created September 19, 2012 23:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Kindari/3753016 to your computer and use it in GitHub Desktop.
Save Kindari/3753016 to your computer and use it in GitHub Desktop.
Locale aware Api routing in Laravel 3
<?php // bundles/api_v1/controllers/example.php
class Api_V1_Example_Controller extends Controller
{
public $restful = True;
public function get_hello($name='World')
{
return "Hello, {$name}!";
}
}
// URL: myapp.com/api/v1/en/example/hello/Kindari
<?php // application/routes.php
// Url: api/v2/locale/controller/method/params
Route::any('api/v(:num)/(:any)/(:any)/(:any)/(:all?)', function($version, $locale, $controller, $method, $rest=''){
Config::set('application.language', $locale);
$bundle = "api_v{$version}";
$params = $rest ? explode('/', $rest) : array();
return Controller::call("{$bundle}::{$controller}@{$method}", $params);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment