Skip to content

Instantly share code, notes, and snippets.

@notomato
Created August 17, 2012 09:54
Show Gist options
  • Save notomato/3377567 to your computer and use it in GitHub Desktop.
Save notomato/3377567 to your computer and use it in GitHub Desktop.
Router configuration examples
<?php
Router::connect('/{:user}/{:controller}/{:action}', array(), function($request) {
if (!Users::count(array('conditions' => array('user' => $request->user)))) {
return false;
}
return $request;
});
Router::connect('/', array(), function($request) {
if (Session::read('user')) {
$location = 'Accounts::index';
} else {
$location = 'Users::add';
}
return new Response(array('status' => 302, 'location' => $location));
});
Router::connect('/photos/view/{:id:[0-9a-f]{24}}.jpg', array(), function($request) {
return new Response(array(
'headers' => array('Content-type' => 'image/jpeg'),
'body' => Photos::first($request->id)->file->getBytes()
));
}
);
// Micro apps
Router::connect('/posts.json', array(), function($request) {
return new Response(array(
'headers' => array('Content-type' => 'application/json'),
'body' => Posts::all()->to('json')
));
});
Router::connect('/posts/{:id}.json', array(), function($request) {
return new Response(array(
'headers' => array('Content-type' => 'application/json'),
'body' => Posts::first($request->id)->to('json')
));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment