1. Go to this page and download the library: Download xsanisty/slim-starter 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/ */
xsanisty / slim-starter example snippets
Route::get('/', function(){
View::display('welcome.twig');
});
/** the Slim way */
$app->get('/', function() use ($app){
$app->view->display('welcome.twig');
});
/** get method */
Route::get('/', 'SomeController:someMethod');
/** post method */
Route::post('/post', 'PostController:create');
/** put method */
Route::put('/post/:id', 'PostController:update');
/** delete method */
Route::delete('/post/:id', 'PostController:destroy');
/** route middleware */
Route::get('/admin', function(){
//route middleware to check user login or redirect
}, 'AdminController:index');
/** Route group to book resource */
Route::group('/book', function(){
Route::get('/', 'BookController:index'); // GET /book
Route::post('/', 'BookController:store'); // POST /book
Route::get('/create', 'BookController:create'); // Create form of /book
Route::get('/:id', 'BookController:show'); // GET /book/:id
Route::get('/:id/edit', 'BookController:edit'); // GET /book/:id/edit
Route::put('/:id', 'BookController:update'); // PUT /book/:id
Route::delete('/:id', 'BookController:destroy'); // DELETE /book/:id
});
/** Route to book resource */
Route::resource('/book', 'BookController');
/** Route to book resource */
Route::controller('/book', 'BookController');
/**
* GET /book will be mapped to BookController:getIndex
* POST /book will be mapped to BookController:postIndex
* [METHOD] /book/[path] will be mapped to BookController:methodPath
*/
class Book Extends Model{}
Class HomeController extends BaseController{
public function welcome(){
$this->data['title'] = 'Some title';
View::display('welcome.twig', $this->data);
}
}
$this->app; //reference to Slim instance
/**
* load local js file located in public/assets/js/application.js
* by default, it will be placed in the last list,
* to modify it, use position option in second parameter
* array(
* 'position' => 'last|first|after:file|before:file'
* )
*/
$this->loadJs('application.js', ['position' => 'after:jquery.js'])
/**
* load external js file, eg: js in CDN
* use location option in second parameter
* array(
* 'location' => 'internal|external'
* )
*/
$this->loadJs('http://code.jquery.com/jquery-1.11.0.min.js', ['location' => 'external']);
/** remove js file from the list */
$this->removeJs('user.js');
/** reset js queue, no js file will be loaded */
$this->resetJs();
/** load local css file located in public/assets/css/style.css */
$this->loadCss('style.css')
/** load external css file, eg: js in CDN */
$this->loadCss('//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css', ['location' => 'external']);
/**
/** publish the variable */
$this->publish('user', User::find(1)->toArray());
/** remove the variable */
$this->unpublish('user');
View::display('welcome.twig', $this->data);
$app->hook('slim.before.route', function(){
//do your hook
});
$app->add(new SomeActionMiddleware());
class SomeActionMiddleware extends Middleware
{
public function call()
{
// Get reference to application
$app = $this->app;
// Run inner middleware and application
$this->next->call();
// do your stuff
}
}