PHP code example of slim / light

1. Go to this page and download the library: Download slim/light library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

slim / light example snippets


$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...

// 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...
});

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());