Enhance Slim framework lightly.
If you have a lot of very long view functions to route, your code may look very messy:
$app->get('/:id', function() use ($app) {
// do somethings...
})
->name('get_book_by_id')
->conditions(array('id' => '\d+'));
$app->post('/:id', function() use ($app) {
// do somethings...
})
->name('edit_book_by_id')
->conditions(array('id' => '\d+'));
$app->delete('/:id', function() use ($app) {
// do somethings...
})
->name('remove_book_by_id')
->conditions(array('id' => '\d+'));
// Other view functions go on...
Being confused by all these tails? In Slim-Light
you can just separate routing and view funtions registering:
// Routing
$app->route('get_book_by_id', '/int:id', 'GET'); // Setup all small tails in one place!
$app->route('edit_book_by_id', '/int:id', 'POST');
$app->route('remove_book_by_id', '/int:id', 'DELETE');
// Registering
$app->set('get_book_by_id', function ($id) use ($app) {
// do somethings...
});
$app->set('edit_book_by_id', function ($id) use ($app) {
// do somethings...
});
$app->set('remove_book_by_id', function ($id) use ($app) {
// do somethings...
});
Tried of writing restful like API? Resource object can ease you pain:
class MovieResource extends \Slim\Light\ResourceController
{
public function get($id) {
echo $id;
}
public function update($id) {
echo $id;
}
public function remove($id) {
echo $id;
}
public function get_all() {
echo 'All movies.';
}
public function create() {
echo 'Create a movie.';
}
}
// Setup all in one line!
$app->resource('movie', '/movie', new MovieResource());
Feel free to open an issue! Waiting for your pull request <3
MIT, head over LICENSE for more informations.