2017 © Pedro Peláez
 

library kecik

Kecik Framework, micro framework

image

kecik/kecik

Kecik Framework, micro framework

  • Monday, October 17, 2016
  • by dnaextrim
  • Repository
  • 3 Watchers
  • 13 Stars
  • 279 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Kecik Framework

PayPal: , (*1)

Rekening Mandiri: 113-000-6944-858, Atas Nama: Dony Wahyu Isprananda, (*2)

Is a framework with a very simple file system, so this is not a complex framework, but you can build and develop this framework to be a complex framework. This Framework support simple MVC where you still have to customize some code for get complex MVC, for Model just generate SQL Query for INSERT, UPDATE and DELETE only, so for code execution that SQL Query please make your self freely as you want or using whichever database libaries. This Framework also support Composer, so as to facilitate you for adding a library from composer., (*3)

Name    : Framework Kecik
Author  : Dony Wahyu Isp
Version : 1.1.0
Country : Indonesian
City    : Palembang

Quickstart, (*4)

The First Step | The Second Step | The Third Step | The Fourth Step, (*5)

More, (*6)

Know More In | Header | Route | Config | Assets | Request | MVC | Controller | Middleware | Model | View | Url | Template, (*7)


The First Step

top, (*8)

Install composer in your opration system, if not installed you can download it from Composer website, after download and initialitation, next you need make composer.json files with contents as follows., (*9)

{
    "require": {
        "kecik/kecik": "1.1.*@dev"
    }
}

next, run this command on console/cmd, (*10)

composer install        

wait a minute until all run without error., (*11)

The Second Step

top, (*12)

Create index.php files or anything, and enter the code below:, (*13)

<?php
require_once "Kecik/Kecik.php";
// or for composer
require_once "vendor/autoload.php";

require "Kecik\Kecik.php" for include system file of framework to the project that you want make. then try run, if only displaying blank page without error message is mean successfull., (*14)

for how to use composer will not be discussed here, you can learn from documentation from composer website, both online and offline., (*15)

The Third Step

top, (*16)

Create variable from Kecik Class as below, (*17)

$app = new Kecik\Kecik();   

then try running back, if not get error is mean you have successfull in this step., (*18)

The Fourth Step

top, (*19)

The next step is make Route for index and run the framework, following code:, (*20)

$app->get('/', function() {
    return 'Hello Kecik';
});

$app->run();

Once the code is written try running, so you can see "Hello Kecik" that mean you have successfull make view for route index/main page for your project., (*21)

The overall appearance code:, (*22)

get('/', function() {
    return 'Hello Kecik';
});

$app->run();
            
```

----

**Know More In**
-------------------------------------------------------------
Header
----------
[top](#kecik-framework)

Header use for do setting a response header
```php
$app->get('hello', function() {
    $this->header(200);
    return 'Hello Kecik';
});
```

----

Route
---------
[top](#kecik-framework)

Route in contained kecik framework current  is get and post, where get and post is request source and that mean is that route just will proccess on match request.For how to use, there are several ways, and very simple is without use Controller, external variable and template, as follow:
```php
$app->get('/', function() {
    return 'Hello Kecik';
});
```
With parameter:
```php
$app->get('hello/:name', function ($name) {
    return 'Hello '.$name;
});
```
Parameter in route use ``:`` at front section, while for optional parameter can use ``(:)``

> **example:** hello/(:name)

With Controller:
```php
$app->get('welcome/:name', new Controller\Welcome($app), function ($controller, $name) use ($app) {
    return $controller->index($name);
});
```     

Ensure that already makes Controller you want to use on that route.

With Template:
```php
$app->get('hello/:name', function ($name) {
    return 'Hello '.$name;
})->template('template_kecik');

$app->get('welcome/:name', new Controller\Welcome($app), function ($controller, $name) use ($app) {
    return $controller->index($name);
})->template('template_kecik');

$app->get('welcome/:name', function($name) {
    $controller = new Controller\Welcome($this);
    return $controller->index($name);
})->template('template_kecik');
```

####**Group**
Kecik Framework also supports grouping route.
```php
$app->group('book', function() {
    $this->post('insert', function() {
        $controller = new Controller\Book($this);
        return $controller->insert();
    });
    
    $this->get('get', function() {
        $controller = new Controller\Book($this);
        return $controller->get();
    });

    $this->post('update', function() {
        $controller = new Controller\Book($this);
        return $controller->update();
    });

    $this->post('delete', function() {
        $controller = new Controller\Book($this);
        return $controller->delete();
    });
    
    $this->post('find', function() {
        $controller = new Controller\Book($this);
        return $controller->find();
    });
});
```
HTML just support method ``POST`` and ``GET``, if we want using method like ``PUT``, ``DELETE``, ``OPTIONS``, and ``PATCH`` we can using do **`Override`**

```html

Note: Applies to the use of the post, put, delete, options, and patch to use the controller and templates there are several steps that need to be prepared, (*23)

is()

To get current value of the route, (*24)

<a href="<?php $this->url->to('home') ?>" <?php echo ($this->route->is() == 'home')? 'class="active"': '' ?>>Home</a>

isPost()

To perform a check whether the request method is POST, if true then the value is TRUE if one then the value is FALSE., (*25)

if ($this->route->isPost() == FALSE)
    $this->header(404);

isGet()

To perform a check whether the request method is GET, if true then the value is TRUE if one then the value is FALSE., (*26)

if ($this->route->isGET() == FALSE)
    $this->header(404);

isPut()

To perform a check whether the request method is PUT, if true then the value isTRUE if one then the value is FALSE., (*27)

if ($this->route->isPut() == FALSE)
    $this->header(404);

isDelete()

To perform a check whether the request method is DELETE, if true then the value is TRUE if one then the value is FALSE., (*28)

if ($this->route->isDelete() == FALSE)
    $this->header(404);

isPatch()

To perform a check whether the request method is PATCH, if true then the value is TRUE if one then the value is FALSE., (*29)

if ($this->route->isPatch() == FALSE)
    $this->header(404);

isOptions()

To perform a check whether the request method is OPTIONS, if true then the value is TRUE if one then the value is FALSE., (*30)

if ($this->route->isOptions() == FALSE)
    $this->header(404);

isAjax()

To perform a check whether the request method is AJAX, if true then the value is TRUE if one then the value is FALSE., (*31)

if ($this->route->isAjax() == FALSE)
    $this->header(404);

First:, (*32)

Setting path or location for assets, application (MVC), and template, following way setting:, (*33)

$app->config->set('path.assets', 'assets');
$app->config->set('path.mvc', 'app');
$app->config->set('path.template', 'templates');

Second:, (*34)

create a folder / directory by setting the path before., (*35)

Third:, (*36)

For folder/directory assets and application sure in which there sub folder/direktori, (*37)

+-- assets
|   +-- css
|   +-- js
|   +-- images

+--app
|  +-- controllers
|  +-- models
|  +-- views

Config

top, (*38)

For a big project and not simple we need some setting/configuration, for setting/configuration this framework also equipped with config, either for setting or to read settings, (*39)

set()

Use set function from config to a set/add value, (*40)

set($key, $value)

paramater $key is parameter key for a setting, (*41)

paramater $value is parameter value for a setting, (*42)

Example:, (*43)

$app->config->set('path.assets', 'assets');

get()

Use get function to get a value from a setting, (*44)

get($key)

parameter $key is key parameter for a setting where to get value, (*45)

Example:, (*46)

$asset_path = $app->config->get('path.assets');

Configuration When Making Kecik Instance

$config = [
    'path.assets'   => 'assets',
    'path.mvc'      => 'app',
    'path.template' => 'templates',
    'error.404'     => 'kecik_template/404',
    'mod_rewrite'   => TRUE,

    'libraries' => [
        'DIC' => ['enable'=>TRUE],
        'Session' => [
            'enable' => TRUE,
            'config' => ['encrypt' => TRUE]
        ],
        'Cookie' => [
            'enable' => TRUE,
            'config' => ['encrypt' => TRUE]
        ],
        'Database' => [
            'enable' => TRUE,
            'config' => [
                'driver' => 'mysqli',
                'hostname' => 'localhost',
                'username' => 'root',
                'password' => '',
                'dbname' => 'kecik'
            ]
        ],
        'MVC' => ['enable' => TRUE],
        'Language' => [
            'enable' => TRUE,
            'params' => [
                'id' => 'language/lang_id.json',
                'us' => 'language/lang_us.json'
            ]
        ]
    ]
];

$app = new Kecik\Kecik($config);

    $app->get('/', function() {
        return 'Hello Kecik';
    });

$app->run();

Assets

top, (*47)

Assets is importan for facilitate us work for add/delete assets as css, js and images, also very useful for a template, and assets juga can be adjusted by controller in used. Assets css and js have same structure while for images is diferrent., (*48)

add()

This function use for add a assets file as css or js., (*49)

add($file='')

paramater $file contains the name of the file that want to load assets, write without using extension, (*50)

Example:, (*51)

$app->assets->css->add('boostrap');
$app->assets->js->add('jquery.min');

delete()

This cunction use to delete a assets file that want to load as css or js., (*52)

delete($file='')

paramater $file contains the name of the file that want to load assets, write without using extension, (*53)

Example:, (*54)

$app->assets->css->delete('boostrap');
$app->assets->js->delete('jquery.min');

render()

This function use to render a asset list or one asset that want to load as css or js, (*55)

render($file='')

paramater $file contains the name of the file that want to load assets, write without using extension, (*56)

Example:, (*57)

echo $app->assets->css->render();
echo $app->assets->js->render();
// atau spesifik render
echo $app->assets->css->render('boostrap');
echo $app->assets->js->render('boostrap.min');

images()

This function use to get link from image assets file., (*58)

images($file)

paramater $file containt image assets file name that want to use., (*59)

Example:, (*60)

<img src="<?php echo $app->assets->images('kecik.jpg'); ?>" />

url()

This function use for get link of assets file for images., (*61)

url()

Request

top, (*62)

Request is other use from $_GET, $_POST and $_SERVER, (*63)

get()

You can use get function to get value from $_GET., (*64)

get($var='')

paramater $var containt name from get variable, (*65)

Example:, (*66)

print_r($this->request->get());
$x = $this->request->get('x');

post()

You can use post function to get value from $_POST., (*67)

post($var='')

paramater $_var containt name from post variable, (*68)

Example:, (*69)

print_r($this->request->post());
$x = $this->request->post('x');

put()

You can use put function to get value from PUT., (*70)

put($var='')

parameters $var contains name from put variable., (*71)

Example:, (*72)

print_r($this->request->put());
$x = $this->request->put('x');

delete()

You can use delete function to get value from DELETE., (*73)

delete($var='')

parameters $var contains name from delete variable., (*74)

Contoh:, (*75)

print_r($this->request->delete());
$x = $this->request->delete('x');

options()

You can use options function to get value from OPTIONS., (*76)

options($var='')

paramater $var berisikan nama dari variabel post, (*77)

Contoh:, (*78)

print_r($this->request->options());
$x = $this->request->options('x');

patch()

You can use patch function to get value from PATCH., (*79)

patch($var='')

paramater $var berisikan nama dari variabel post, (*80)

Contoh:, (*81)

print_r($this->request->patch());
$x = $this->request->patch('x');

file()

You can use file function to get value from $_FILE, (*82)

file($file)

parameters $file contains the name of the variable FILES, (*83)

Contoh:, (*84)

$x = $this->request->file('photo')->move($source, $destination);

server()

You can use server function to get value from $_SERVER, (*85)

server($var='')

paramater $var containt name from server variable, (*86)

Exmple:, (*87)

print_r($this->request->server());
$host = $this->request->server('HTTP_HOST');

MVC

This Framework also support simple MVC, where route will call Controller and Controller will call Model or/and View., (*88)

Controller

top, (*89)

For make controller is simple, we just make file with name as controller name and save into directory that setting before via config, The following simple code a controller, (*90)

get('/', new Controller\Welcome(), function($controller) {

});
```

- **With Parameter**
To use the controller parameters on the way is also quite easy, just add a parameter to the constructor and when the controller is live input parameter constructor.
```php
dbcon = $dbcon;
    }
}
```

Next how to use at route as follow:
```php
$app->get('/', new Controller\Welcome($dbcon), function($controller) {

});
```

- **With Method/Function**
For use method/function in controller is simple, just call in callback section route. Here's how to write the code controller using the method / function.
```php
dbcon = $dbcon;
    }

    public function index() {
        return 'Kecik berkata: Controler->index()';
    }
}
```

Next use method/function in route is as follow.
```php
$app->get('/', new Controller\Welcome($dbcon), function($controller) {
    return $controller->index();
});
```

- **With parameter in Method/Function**
For give paramter in Method/Fungsi in controller we can give at the time of the call method in callback route, The following code example controller with a method / function parameterized.
```php
dbcon = $dbcon;
    }

    public function index() {
        return 'Kecik berkata: Controler->index()';
    }

    public function hello($nama) {
        return "Hello, $nama";
    }
}
```

How to use in route as follow.
```php
$app->get('/hello/:nama', new Controller\Welcome($dbcon), function($controller, $nama) {
    return $controller->index($nama);
});
```

Middleware
-----------------
[top](#kecik-framework)
Middleware is functions will running before/after callback execution on route.
```php
$mw1 = function() {
    echo 'is Middleware 1 [Before]';
};
$mw2 = function() {
    echo 'is Middleware 2 [Before]';
};
$mw3 = function() {
    echo 'is Middleware 3 [After]';
};

$mw4 = function() {
    echo 'is Middleware 4 [After]';
};

$app->get('middleware', array($mw1, $mw2), function() {
    return 'is Response Middleware Route';
}, array($mw3, $mw4));
```

Model
-------
[top](#kecik-framework)

For make model is simple, we just create a file with name as model name and save in directory that setting before via config, The following simple code model
```php
dbcon = $dbcon;
    }

    public function index() {
        return 'Kecik berkata: Controler->index()';
    }

    public function hello($nama) {
        return "Hello, $nama";
    }

    public function insert() {
        $model = new \Model\Data();
            $model->nama = $_POST['nama'];
            $model->email = $_POST['email'];
        $sql = $model->save();
    }

    public function update($id) {
        $model = new \Model\Data(array('id'=>$id));
            $model->nama = $_POST['nama'];
            $model->email = $_POST['email'];
        $sql = $model->save();
    }

    public function delete($id) {
        $model = new \Model\Data(array('id'=>$id));
        $sql = $mode->delete();
    }
}
```
 
 View
------
[top](#kecik-framework)

For make view is also simple, because in this you don't need make class/object, but just plain php file will call by controller, the following code view.
```php


How to use view file in controller are as follows., (*91)

view('welcome');
    }
}
```

How to send variable to view are as follows:
```php
view('welcome', array('nama'=>$nama));
    }
}
```

**HMVC**
-------------
Kecik Framework juga mendukung HMVC bahkan HMVC dengan struktur yang lebih dinamis.
Contoh Struktur HMVC:
```
+--app
|  +-- controllers --+
|  +-- models        |-- MVC
|  +-- views       --+
|  +-- module           --+
|      +-- controllers    |-- HMVC
|      +-- models         |
|      +-- views        --+
|  +-- tim1  ---------------------------------------+------------+
|      +-- module1                                  |            |
|          +-- controllers  --+                     |            |
|          +-- models         |-- MVC Tim1\Module1  |            |
|          +-- views        --+                     |-- Tim1     |
|      +-- module2                                  |            |
|          +-- controllers  --+                     |            |
|          +-- models         |-- MVC Tim1\Module2  |            |
|          +-- views        --+---------------------+            |-- HMVC Dinamis
|  +-- tim2  ---------------------------------------+            |
|      +-- module1                                  |            |
|          +-- controllers  --+                     |            |
|          +-- models         |-- MVC Tim2\Module1  |            |
|          +-- views        --+                     |-- Tim2     |
|      +-- module2                                  |            |
|          +-- controllers  --+                     |            |
|          +-- models         |-- MVC Tim2\Module2  |            |
|          +-- views        --+---------------------+------------+
```

**Controller dalam HMVC**
Untuk Controller pada HMVC penamaan namespace harus sama dengan struktur direktori MVC nya.
```php
view('index');
    }
}
```

atau untuk HMVC Dinamis
```php
view('index');
    }
}
```

Cara menggunakan controller HMVC pada route
```php
$app->get('welcome', function() {
    $ctrl = new \Module\Controller\Welcome();
    return $ctrl->index();
});
```
Sedangkan untuk HMVC Dinamis
```php
$app->get('welcome', function() {
    $ctrl = new \Tim1\Module1\Controller\Welcome();
    return $ctrl->index();
});
```


**Model dalam HMVC**
Sama dengan Controller penamaan namespace harus sama dengan suktrur direktori MVC nya.
```php
name = $this->request->post('name');
            $data->address = $this->request->post('address');
            $data->email = $this->request->post('email');
        $data->save();
    }
}
```

atau untuk HMVC Dinamis
```php
name = $this->request->post('name');
            $data->address = $this->request->post('address');
            $data->email = $this->request->post('email');
        $data->save();
    }
}
```

Kita bisa juga menggunakan Model dari module lain ataupun dari model utama yang berada diluar direktori module

**View dalam HMVC**
Menggunakan file pada masing module tidak ada perbedaan dengan cara MVC, jadi kita tinggal memanggil nama file view nya tanpa
disertakan dengan ekstensi .php
```php
view('wellcome', array('nama'=>$nama));
    }
}
```

Sedangkan jika kita ingin menggunakan view dari module lain atau mungkin pada view utama yang berada diluar direktori module,
kita cukup mengubah nilai pada parameter pertama menjadi array, dimana nilai index pertama adalah nama view jika ingin 
menggunakan view utama yang berada di luar direktori module, atau index pertama adalah nama module dan index kedua adalah
nama view dari module yang view nya ingin digunakan.
```php
$this->view(array('nama_view'));  // Menggunakan view utama
$this->view(array('nama_module', 'nama_view')); // Menggunakan view dari module lain
```

```php
view(array('Module2', 'welcome'), array('nama'=>$nama));
    }
}
```

atau
```php
view(array('welcome'), array('nama'=>$nama));
    }
}
```

---
Url
----
[top](#kecik-framework)

Url is important for help jobs for get value like protocol, base path, base url, also redirect or make link to other route .

####**protocol()**
For get Protocol value
```php
echo $this->url->protocol();
```
####**basePath()**
For get Base Path value
```php
echo $this->url->basePath();
```
####**baseUrl()**
For Get Base Url value
```php
echo $this->url->baseUrl();
```
####**redirect($route)**
For redirect to other route
```php
$this->url->redirect('login');
```
####**to($route)**
For echo url with route
```php

linkTo($route)

For Get Url with Route value, (*92)

<a href="<?php echo $this->linkTo('home') ?>">Home</a>

Template

top, (*93)

For make template in this framework is also simple, you just create template file in directory that you setting before via config. Here is a simple example of code templates, (*94)


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Simple Template</title>

        <meta name="description" content="overview &amp; stats" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        @css
    </head>
    <body>

    <div class="container">

        @response

    </div>

        @js
    </body>
</html>

Sign {{ dan }} just subtitute tag <?php and > for echo you can use {{= it's same <?php echo while want using sign {{ and }} for AngularJS you can use sign \ after its, example \{{ atau \}}, this just simple template engine, but you use php tags., (*95)

The @response atau @yield is to put the output of the controller., (*96)

The @css or @js is to apply the template rendering assets, (*97)

How to use the template on the route is as follows., (*98)

<?php
$app->get('welcome/:nama', new Controller\Welcome(), function ($controller, $nama) {
    return $controller->welcome($nama);
})->template('template');

Replace Template, (*99)

<?php
$app->get('admin', function() {
    if (!isset($_SESSION['login'])) {
        //** Replace Template
        $this->template('login', TRUE);
    } else {
        $controller = new Controller\Admin($this);
        return $controller->index();
    }
})->template('template');

The Versions

17/10 2016

dev-develop

dev-develop

Kecik Framework, micro framework

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Dony Wahyu Isp

micro framework single kecik

20/07 2016

dev-master

9999999-dev

Kecik Framework, micro framework

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Dony Wahyu Isp

micro framework single kecik

20/05 2015

1.1.0

1.1.0.0

Single File Micro Framework

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Dony Wahyu Isp

micro framework single kecik

07/04 2015

1.0.3-beta

1.0.3.0-beta

Single File Framework

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Dony Wahyu Isp

micro framework single kecik