dev-master
9999999-dev
GPL-3.0+ LGPL-2.1
The Requires
by Yusuf Alija
Wallogit.com
2017 © Pedro Peláez
Simple php routing class for quick bootstrapping. Slight is just a small wrapper class which helps you easily separate logic from markup, its minimal just a single class, and very fast. Slight uses Twig for templating which is awesome., (*1)
Via composer, (*2)
composer require slight/slight
First create an .htaccess file where your index.php is and add this, (*3)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
get('/', function() {
return 'Hi Home';
});
//Run slight
$app->run();
```
###Basic routing
```php
get('/', function() {
return 'Hi Home';
});
$app->get('/admin/', function() {
return 'Hi Admin';
});
//Run slight
$app->run();
```
###Parameters
```php
get('/admin/:username/', function($username) {
return 'Hello '.$username;
});
$app->get('/admin/:firstname/:lastname/', function($firstname, $lastname) {
return 'Welcome '.$firstname.' '.$lastname;
});
//Run slight
$app->run();
```
###Redirects
```php
get('/admin', function() use ($app) {
$app->redirect('/admin/login/');
});
$app->get('/admin/login', function() use ($app) {
return 'Login page';
});
//Run slight
$app->run();
```
###Rendering output to file
All variables passed to the render function will be mapped
to a $display variable which can be used to output content.
```php
get('/foo', function() {
return $app->render('foo.php', array(
'foo' => 'Bar'
));
});
$app->run();
```
####In foo.php
```php
//Will output Bar
When you install slight you will notice that Twig is also installed as a dependency. That is because Slight can also work closely with Twig., (*4)
<?php
//Require slight
require "vendor/autoload.php";
//Configure the templates folder
$app = new SlightApp(array(
'templates' => 'views'
));
$app->get('/', function() use ($app) {
return $app->render('foo.html', array(
'foo' => array(
'name' => 'Bar',
'username' => 'baz'
)
));
});
//Run slight
$app->run();
{{ foo.name }} {{ foo.username }}, (*5)
<?php
//Require slight
require "vendor/autoload.php";
//Instantiate slight
$app = new SlightApp();
//Connect to mongodb using singleton
$app->singleton("database", function() use ($app) {
//this will create $database property in slight
//which will map to a testDatabase in MongoDB
//this property can then be accessible using $app->database
//Similar connections can be made to a MySQL database too.
$connection = new MongoClient();
return $connection->testDatabase;
});
//Home route
$app->get('/', function() use ($app) {
//Find users from database, using property created from singleton.
$users = $app->database->Users->find();
//Render them
return $app->render('users.html', array(
"users" => $users
));
});
//Run slight
$app->run();
GPL-3.0+ LGPL-2.1