PHP code example of kecik / mvc

1. Go to this page and download the library: Download kecik/mvc 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/ */

    

kecik / mvc example snippets



fig = [
    //** Path of Assets
    'path.assets'   => 'assets',
    //** Path of MVC
    'path.mvc'      => 'app',
    //** Path of Template
    'path.template' => 'templates',
    
    //** Load Libraries
    'libraries' => [
        //-- DIC Library
        'DIC' => ['enable' => TRUE],
        //-- Database Library
        'Database' => [
            'enable' => TRUE,
            'config' => [
                'driver' => 'mysqli',
                'hostname' => 'localhost',
                'username' => 'root',
                'password' => '',
                'dbname' => 'kecik'
            ]
        ],
        //-- MVC Library
        'MVC' => ['enable' => TRUE]
    ]
    //-- End Libraries
];

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

    //** Assets
    //-- CSS Assets
    $app->assets->css->add('bootstrap.min');
    $app->assets->css->add('bootstrap-theme.min');
    $app->assets->css->add('starter-template');
    //-- END CSS Assets
    //-- END Assets

    //** Connect to Database
    $app->db->connect();

    //** DIC Container
    //-- User Controller
    $app->container['userController'] = function($container) {
        return new Controller\User();
    };
    //-- END
    
    //** Index
    $app->get('/', function() {
        return $this->container['userController']->index();
    })->template('bootstrap_template');
    
    //** User List
    $app->get('user', function() {
        return $this->container['userController']->read();
    })->template('bootstrap_template');

    //** FORM
    //-- FORM Add
    $app->get('add', function() {
        return $this->container['userController']->form();
    })->template('bootstrap_template');
    //-- FORM Update
    $app->get('edit/:username', function($username) {
        return $this->container['userController']->form($username);
    })->template('bootstrap_template');
    //-- END FORM

    //** Action INSERT, UPDATE, AND DELETE
    //-- INSERT
    $app->post('insert', function() {
        return $this->container['userController']->insert();
    });
    //-- UPDATE
    $app->post('update/:username', function($username) {
        return $this->container['userController']->update($username);
    });
    //-- DELETE
    $app->get('delete/:username', function($username) {
        return $this->container['userController']->delete($username);
    });
    //-- END Action

$app->run();


namespace Controller;

use Kecik\Controller;

class User extends Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        return $this->view('index');
    }

    public function read() {
        return $this->view('read');
    }

    public function form($id='') {
        if ($id=='') 
            $url = $this->url->linkto('insert');
        else
            $url = $this->url->linkto('update/'.$id);

        return $this->view('form', ['id'=>$id, 'url'=>$url]);
    }

    public function insert() {
        $request = $this->request;
        $user = new \Model\User();
            $user->username = $request->post('username');
            $user->password = md5($request->post('password'));
            $user->fullname = ucwords($request->post('fullname'));
            $user->email = $request->post('email');
        $user->save();
        $this->url->redirect('user');
    }

    public function update($id) {
        $request = $this->request;
        $user = new \Model\User(['username'=>$id]);
            $user->username = $request->post('username');
            $user->password = md5($request->post('password'));
            $user->fullname = ucwords($request->post('fullname'));
            $user->email = $request->post('email');
        $user->save();
        $this->url->redirect('user');
    }

    public function delete($id) {
        $request = $this->request;
        $user = new \Model\User(['username' => $id]);
        $user->delete();
        $this->url->redirect('user');
    }
}


namespace Model;

use Kecik\Model;

class User extends Model {
    protected static $table = 'user';

    public function __construct($id='') {
        parent::__construct($id);
    }
}
html
<br />
<a href=" $this->url->to('index.php/add') 
html

  if ($id != '') {
    $rows = Model\User::find([
      'where' => [
        ['username', '=', $id]
      ]
    ]);
    foreach ($rows as $data) {
      $username = $data->username;
      $fullname = $data->fullname;
      $email = $data->email;
    }
  }