Lean PHP Framework
Lean PHP Framework is a micro framework PHP (~40KB). Modern frameworks are powerfull but so much complicated,
the mostly of resources you never gonna use, some functionality sounds good but if you don't really need it's a
only waste of time.
With Lean you can construct fast e lightweight softwares, with follows resources:, (*1)
- Structure MVC, REST or both;
- Requests;
- Routes (automatic or custom);
- Namespaces;
- Class autoload;
- PHP code hidden;
- Basic template engine;
- Date and Time manipulation;
- Easy configuration;
Requirement
PHP 5.3+, (*2)
Basic structure
-- rootdir
-- app
-- main (module)
-- controllers
-- HomeController.php
-- models
-- views
-- home
-- index.phtml
-- Bootstrap.php
-- Routes.php
-- public_html
-- css
-- js
-- img
-- index.php
-- .htaccess
-- vendor
-- composer
-- lean
-- autoload.php
Create into your rootdir teh follows directories:, (*3)
-
app: You will write all your application php into app directory (controllers, models, views and configs), this way your application not stay exposed., (*4)
-
public_html: Into public_html directory we have only index.php as file .php. You can put all yours public files, like css, javascripts, images, fonts, etc., (*5)
-
vendor: Composer will create it and copy our lib to lean directory., (*6)
Getting started
Instalation
Install via Composer, (*7)
composer require lean/lean
Easy configuration
create file index.php into public_html directory, (*8)
Into index.php we have only one line, all of rest application php keep safe into app directory., (*9)
<?php require_once '../app/Bootstrap.php'; ?>
```
create file `.htaccess` into **public_html** directory to custom urls works
> Don't forget enable mod_rewrite on apache
```bash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Create file Bootstrap.php into app directory, (*10)
run();
```
Well done! It's all configuration necessary to run like a pro.
### Hello world
Regardless of whether their application is rest or not, I think is a good ideia keep your access logic always into controllers,
into Routes.php you keep only routes ;)
```php
Remember, in your site type only **www.your-domain.com**, everything else php is hidden.
## Automatic route controller
http://localhost/rootdir/public_html/`$1`/`$2`/`$3`
* `$1` : Module - if not informed, use main module (main directory)
* `$2` : Controller - if not informed, instance indexController class
* `$3` : Method - if not informed, call index method
```php
uri `/main/product` result is **About Product!**
> uri `/main/product/index` result is **About Product!**
> uri `/main/product/features-list` result is **Product list!**
> uri `/main/product/features_list` result is **Product list!**
> uri `/main/product/buy` result is **Processing your order...**
> uri `/main/product/buy-action` result is **Processing your order...**
> uri `/main/product/buy_action` result is **Processing your order...**
To IndexController example, the result is:
> uri `/` result is **Hello World!**
> uri `/main` result is **Hello World!**
> uri `/main/index` result is **Hello World!**
> uri `/main/index/index` result is **Hello World!**
## Custom routes
### Config routes file
In `app/Bootstrap.php` add file routes before launch Lean
```php
...
/**
* routes file
*/
Lean\Route::set_routes_path('app/Routes.php');
/**
* init lean framework
*/
Lean\Launch::instance()->run();
```
### Basic route
create file `Routes.php` into **app** directory
```php
Url: http://your-site.com/foo/bar // result is 'Hi'
### Route to method in controller
```php
'product',
));
Route::set('resources', array(
'controller' => 'product',
'method' => 'resources_list'
));
Route::set('learn-more-about-product', array(
'controller' => 'product',
'method' => 'resources_list'
));
```
> Url `http://your-site.com/product` result is **About Product**
> Url `http://your-site.com/resources` result is **Product List**
> Url `http://your-site.com/learn-more-about-product` result is **Product List**
### Route to different module
```php
'api'
'controller' => 'payment',
));
```
### Simple route alias
```php
Route::alias('old-page-about-product', 'product');
```
### Multiple route alias
```php
Route::alias(array('old-page-about-product', 'foo', 'bar'), 'product');
```
## Request object
Recovery request data in controllers
```php
request->name;
echo $this->request->last_name;
/**
* get only method post - same of variable $_POST
*/
echo $this->request()->post()->name;
/**
* get only method post - same of variable $_POST
*/
echo $this->request()->get()->name;
/**
* get only method file - same of variable $_FILE
*/
$request = $this->request()->file()->name;
/**
* you can too instance request object
*/
$request = new \Lean\Http\Request();
$request->name
...
}
}
```
## Using Views
In views directory, you must create `product` and `layout` subdirectories with `.phtml` files.
```php
...
-- controllers
-- ProductController.php
-- models
-- views
-- product
-- index.phtml
-- edit.phtml
-- layout
-- header.phtml
-- footer.phtml
-- template.html
...
```
Create `template.phtml` in layout directory, you can include header and footer parts here
```html
My new app
$this->app->view->render('layout.header') ?>
<div id="container">
<!-- include page setted in content variable via ProductController -->
<? $this->app->view->make('content') ?>
</div>
<!-- include footer.phtml from layout directory
<? $this->app->view->render('layout.footer') ?>
</body>
</html>
Rendering yours views, (*11)
<?php
namespace app\main\controllers;
class ProductController extends \Lean\App
{
public function index()
{
/**
* set which page will rendered by "content" variable in "template.html"
* by default, if informed only "index" will rendered .phtml file into product directory
*/
$this->view()->set('content', 'index');
/*
* render template
*/
$this->view()->render('layout.template');
}
public function edit()
{
/**
* this example will rendered "/product/edit.phtml" file
*/
$this->view()->set('content', 'edit');
/*
* render template
*/
$this->view()->render('layout.template');
}
}
Date
Date::FORMAT_DATE = 'YYYY-mm-dd';
Date::FORMAT_DATE_TIME = 'YYYY-mm-dd HH:MM:SS';
Date::FORMAT_DATE_USER = 'dd/mm/YYYY';
Date::FORMAT_DATE_TIME_USER = 'dd/mm/YYYY HH:MM:SS';
Date::FORMAT_DATE_LONG = 'Sexta-feira, 30 de janeiro de 2015';
Date::FORMAT_DAY = 'dd';
Date::FORMAT_MONTH = 'mm';
Date::FORMAT_YEAR = 'YY';
Date::FORMAT_TIME = 'HH:MM:SS';
Date::FORMAT_TIME_SHORT = 'HH:MM';
Date::FORMAT_DATE_TIME_HASH = 'YYYYmmdd_HHMMSS';
-
YYYY : Year 4 digits
-
mm : Month 2 digits
-
dd : Day 2 digits
-
HH : Hours 2 digits
-
MM : Minutes 2 digits
-
SS : Seconds 2 digits
Now
Print today date, (*12)
use Lean\Format\Date as Date;
echo Date::now() // "YYYY-mm-dd HH:MM:SS"
echo Date::now(Date::FORMAT_DATE_USER) // "dd/mm/YYYY"
echo Date::now(Date::FORMAT_TIME) // "HH:MM:SS"
Format using constants date, (*13)
echo Date:format('2015-01-30 10:59:59', Date::FORMAT_DATE) // 2015-01-30
echo Date:format('2015-01-30 10:59:59', Date::FORMAT_DATE_TIME) // 2015-01-30 10:59:59 (nothing change)
echo Date:format('2015-01-30 10:59:59', Date::FORMAT_DATE_USER) // 30/01/2015
echo Date:format('2015-01-30 10:59:59', Date::FORMAT_DATE_TIME_USER) // 30/01/2015 10:59:59
echo Date:format('2015-01-30 10:59:59', Date::FORMAT_TIME_SHORT) // 10:59
Format to users, (*14)
echo Date:format_to_human('2015-01-30 10:59:59') // 30/01/30 10:59:59
echo Date:format_to_human('2015-01-30') // 30/01/30
echo Date:format_to_human('30/01/2015 10:59:59') // 30/01/30 10:59:59 (nothing change)
echo Date:format_to_human('30/01/2015') // 30/01/30 (nothing change)
echo Date:format_to_human('30/01/2015 10:59:59', Date::FORMAT_DATE_USER) // 30/01/30
echo Date:format_to_human('2015-01-30 10:59:59', Date::FORMAT_DATE_USER) // 30/01/30
Format to datebase, (*15)
echo Date:format_to_machine('2015-01-30 10:59:59') // 2015-01-30 10:59:59 (nothing change)
echo Date:format_to_machine('2015-01-30') // 2015-01-30 (nothing change)
echo Date:format_to_machine('30/01/2015 10:59:59') // 2015-01-30 10:59:59
echo Date:format_to_machine('30/01/2015') // 2015-01-30
echo Date:format_to_machine('30/01/2015 10:59:59', Date::FORMAT_DATE) // 2015-01-30
echo Date:format_to_machine('2015-01-30 10:59:59', Date::FORMAT_DATE) // 2015-01-30
Validating date
Validate date format, (*16)
echo Date::validate('01/01/2015'); // true
echo Date::validate('01/01/2015 12:10:00'); // true
echo Date::validate('2015-01-01'); // true
echo Date::validate('2015-01-01 12:10:00'); // true
echo Date::validate('201-501-01'); // false
echo Date::validate('foo'); // false
Validate especific date user format (00/00/0000 00:00:00), (*17)
echo Date::validate_format_human('01/01/2015'); // true
echo Date::validate_format_human('01/01/2015 12:10:00'); // true
echo Date::validate_format_human('2015-01-01'); // false
echo Date::validate_format_human('2015-01-01 12:10:00'); // false
echo Date::validate_format_human('201-501-01'); // false
echo Date::validate_format_human('foo'); // false
Validate especific date datebase format (0000-00-00 00:00:00), (*18)
echo Date::validate_format_machine('01/01/2015'); // false
echo Date::validate_format_machine('01/01/2015 12:10:00'); // false
echo Date::validate_format_machine('2015-01-01'); // true
echo Date::validate_format_machine('2015-01-01 12:10:00'); // true
echo Date::validate_format_machine('201-501-01'); // false
echo Date::validate_format_machine('foo'); // false
Time
Time::FORMAT_HOUR_MINUTES = 'HH:MM';
Time::FORMAT_HOUR_MINUTES_SECONDS = 'HH:MM:SS';
Time::FORMAT_HOUR = 'HH';
Time::FORMAT_MINUTES = 'MM';
Time::FORMAT_SECONDS = 'SS';
-
HH : Hours 2 digits
-
MM : Minutes 2 digits
-
SS : Seconds 2 digits
Now
Print time at moment, (*19)
use Lean\Format\Time as Time;
echo Time::now() // "HH:MM:SS"
echo Time::now(Time::FORMAT_HOUR_MINUTES) // "HH:MM"
echo Time::now(Time::FORMAT_HOUR) // "HH"
Format time default, (*20)
echo Time::format('12:10') // 12:10:00
echo Time::format('122:10') // 122:10:00
echo Time::format('12') // 12:00:00
echo Time::format('12:60') // 12:59:00
echo Time::format('12:99:99') // 12:59:59
echo Time::format('1:1') // 01:01:00
echo Time::format('5:30') // 05:30:00
Format using constants time, (*21)
echo Time::format('12:10', Time::FORMAT_HOUR_MINUTES_SECONDS) // 12:10:00
echo Time::format('12:10:5', Time::FORMAT_HOUR_MINUTES_SECONDS) // 12:10:05
echo Time::format('12', Time::FORMAT_HOUR_MINUTES_SECONDS) // 12:00:00
echo Time::format('12:10:15', Time::FORMAT_HOUR_MINUTES) // 12:10
echo Time::format('12', Time::FORMAT_HOUR_MINUTES) // 12:00
echo Time::format('2:10:15', Time::FORMAT_HOUR_MINUTES) // 02:10
Converter time
Convert time to seconds, (*22)
echo Time::time_to_seconds('01:30:00') // 5400
echo Time::time_to_seconds('01:15:00') // 4500
echo Time::time_to_seconds('00:01:15') // 75
echo Time::time_to_seconds('48:00:00') // 172800
Convert seconds to time, (*23)
echo Time::seconds_to_time('5400') // 01:30:00
echo Time::seconds_to_time('4500') // 01:15:00
echo Time::seconds_to_time('75') // 00:01:15
echo Time::seconds_to_time('172800') // 48:00:00
Calculate time
Sum time, (*24)
echo Time::sum('01:15:00', '02:30:05'); // 03:45:05
echo Time::sum('12:30:00', '07:00:00'); // 19:30:00
echo Time::sum('12:30:00', '12:00:00'); // 00:30:00
Subtract time, (*25)
echo Time::subtract('02:30:05', '01:15:00'); // 01:15:05
echo Time::subtract('12:30:00', '07:00:00'); // 05:30:00
echo Time::subtract('12:30:00', '13:00:00'); // 23:30:00
Author
Created by Dyorg Almeida, a full-stack web developer and PHP expert (Zend Certified Engineer).
Today Dyorg is founder and CTO at Rabbiit, a brazilian startup that develops a simple time tracking software., (*26)
http://rabbiit.com - Sign up for free (available only in portuguese for now), (*27)
License
The Lean PHP framework is released under MIT public license.
http://www.opensource.org/licenses/MIT
Copyright (c) 2015, (*28)