2017 © Pedro Peláez
 

library slight

image

slight/slight

  • Tuesday, January 17, 2017
  • by aliasc
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

slight

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)

Installation

Via composer, (*2)

composer require slight/slight

Usage

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]

Instantiate slight

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

Using Twig to render output

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

In foo.html now using Twig



{{ foo.name }} {{ foo.username }}, (*5)

Using singleton to connect to a database

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

The Versions

17/01 2017

dev-master

9999999-dev

  Sources   Download

GPL-3.0+ LGPL-2.1

The Requires

 

by Yusuf Alija

17/01 2017

1.0

1.0.0.0

  Sources   Download

GPL-3.0+ LGPL-2.1

The Requires

 

by Yusuf Alija