1. Go to this page and download the library: Download illogical/lava 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/ */
// URL: http://example.com/foo1.bar/foo2.bar/foo3.bar/foo4.bar
App ::route('/:node1/#node2/*node3')
->to (function($node1, $node2, $node3) { // handler
echo $node1; # foo1.bar
echo $node2; # foo2
echo $node3; # foo3.bar/foo4.bar
});
// route search
App::routes_match();
// environment constraint
App::route('/foo', [
'method' => ['GET', 'HEAD'], // if the method is GET or HEAD
'user_addr' => '127.0.0.1', // and the user is local
'user_agent' => '/^Mozilla/', // and the browser is Mozilla
]);
// method restriction only
App::route('/foo', 'DELETE');
App::route_get ('/foo');
// analog
App::route ('/foo', 'GET');
// function
App::route('/foo')->to(function() {echo 'hello';});
// class|namespace, method
App::route('/foo')->to('Controller\Foo', 'bar');
// file, method
// the class name must match the file name
// an instance of the Foo class will be created and the bar method will be called
App::route('/foo')->to('controller/Foo.php', 'bar');
// file, class|namespace, method
App::route('/foo')->to('controller/Foo.php', 'Ctrl\Foo', 'bar');
// if the class is different from the file name or if we need to specify a namespace
App::route('/page')->to(function() {
App::render([
'html' => 'HTML CONTENT',
'json' => ['bar' => 123],
function () {
echo 'OTHER TYPE: ' . App::type();
},
]);
});
// URL: http://example.com/page.html
App::routes_match(); # HTML CONTENT
// URL: http://example.com/page.json
App::routes_match(); # {"bar":123}
// URL: http://example.com/page.xml
App::routes_match(); # OTHER TYPE: xml
// if Lava\App::conf()->type == 'html'
// URL: http://example.com/page
App::routes_match(); # HTML CONTENT
$data = $storage
->factory('users')
->filter('id', 123)
->del();
# query: DELETE FROM `users` WHERE `id` = ?
# bind: 123
$data = $storage
->factory('users')
->filter_gt('id', 123)
->count('email');
# query: SELECT COUNT(`email`) AS `val` FROM `users` WHERE `id` > ?
# bind: 123
$data = $storage
->factory('users')
->filter_like('email', '%@mail%')
->min('id');
# query: SELECT MIN(`id`) AS `val` FROM `users` WHERE `email` LIKE ?
# bind: '%@mail%'
$data = $storage
->factory('users')
->filter_in('role_id', [2, 3])
->max('id');
# query: SELECT MAX(`id`) AS `val` FROM `users` WHERE `role_id` IN (?, ?)
# bind: 2, 3
$data = $storage
->factory('users')
->avg('id');
# query: SELECT AVG(`id`) AS `val` FROM `users`
$data = $storage
->factory('users')
->sum('id');
# query: SELECT SUM(`id`) AS `val` FROM `users`
use Lava\Model\SQL;
class User extends SQL {
// meta data
protected static
// optionally, storage name
// name from Lava\App::conf()->storage()
// default is "0"
$storage = '0',
// optionally, table name
// defaults to a class name in snake case with an "s" at the end
$table = 'users',
// optionally, the name of the primary key
// defaults to "id"
$id = 'id',
// columns description
$columns = [
'key' => [
// value cannot be NULL
'not_null' => FALSE|TRUE,
// value is unique
'unique' => FALSE|TRUE,
// default value
'default' => VALUE,
// validation tests
'valid' => [tests],
],
...
],
// optionally, default limit
// if not specified at selection
// unrestricted by default
$limit = 0;
}
// selecting one object by ID
$user = User::one(123);
echo $user->name;
// selecting a collection by condition
$users = User::find(['active' => TRUE])->get();
foreach ($users as $user) {
echo $user->name;
}
class Session extends SQL {
// method name must start with the prefix "column_default_"
public static function column_default_created () {
return date('Y-m-d H:i:s');
}
}
$session = new Session;
echo $session->created; // now
class User extends SQL {
public function sessions () {
return $this
->has_many(Session::classname());
}
// additional filters
public function active_sessions () {
return $this
->sessions()
->filter(['active' => TRUE]);
}
}
class Session extends SQL {
public function user () {
return $this->belongs_to(User::classname());
}
}
$user = User::one(123);
// property returns a collection
foreach ($user->sessions as $session) {
echo $session->id;
}
// method returns resultset
$active = $user ->sessions()
->filter(['active' => TRUE])
->get();
// similarly
$active = $user ->active_sessions;
class Session extends SQL {
public static function import ($data) {
if (isset($data['ip'])) {
$data['ip'] = long2ip($data['ip']);
}
return $data;
}
public static function export ($data) {
if (isset($data['ip'])) {
$data['ip'] = ip2long($data['ip']);
}
return $data;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.