PHP code example of vgalvoso / spm

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

    

vgalvoso / spm example snippets


//routes.php
use App\Controller\Home;

get('',Home::class,"index");

//routes.php
use App\Controller\Home;

//post(route name,class,static function to call)
post('post',Home::class,"samplePost");

//routes.php
use App\Controller\Home;

//get(route name,class,static function to call)
get('get',Home::class,"sampleGet");

//routes.php sample
cli('comand',Command::class,"executeCommand");

public static function samplePost($post_data){
    //now you can get values from POST request
    //sample: $username = $post_data["username"];
}

public static function sampleGet($get_data){
    //now you can get values from GET request
    //sample: $id = $get_data["user_id"];
}

//app/Controller/Home.php
public static function index()
{
    view("section/header");
    view("home");
    view("section/footer");
}

//app/Controller/Home.php
public static function index()
{
    $data = ["header" => "Simple MVC Framework",
            "sub_header" => "Just what you need!"];
    view("section/header");
    view("home",$data);
    view("section/footer");
}

//app/Controller/Home.php
$home_model = new HomeModel();

//app/Controller/Home.php
$home_model = new HomeModel("ms");

//app/Controller/Home.php
$home_model = new HomeModel(null,$host,$user,$pass,$dbname,$driver(mysql/mssql));

//app/Model/HomeModel.php
public function getUser(){
    $query = "SELECT * FROM users WHERE id = 1";
    return $this->getItem($query);
}

//app/Model/HomeModel.php
public function getAllUsers(){
    $query = "SELECT * FROM users";
    return $this->getItems($query);
}

//app/Model/HomeModel.php
public function validateUser($username,$password){
    $query = "SELECT * FROM users WHERE username = :uname AND pass = :pass";
    $params = ["uname" => $username, "pass"=>$password];
    return $this->getItem($query,$params);
}

//app/Model/HomeModel.php
public function addUser($username,$password,$firstname){
    $params = ["u_username"=>$username,
            "u_password"=>password_hash($password,PASSWORD_DEFAULT),
            "firstname"=>$firstname];
    if($this->insert("users",$params))
        return true;
    return false;
}

//app/Model/HomeModel.php
public function deleteUser($userId){
    $params = ["id" => $userId];
    $query = "DELETE FROM users WHERE id = :id";
    $this->exec($query,$params);
}

//app/Model/HomeModel.php
public function updateUser($firstname,$userId){
    $query = "UPDATE users SET firstname = :firstname WHERE id = :userId";
    $params = ["firstname" => $firstname,"userId"=>$userId];
    return $this->exec($query,$params);
}

//inside your model
$this->startTrans();

$this->commit();

$this->rollback();

$this->getError();

$this->getDriver();

$this->lastId();