Skip to content

Commit

Permalink
beginning of major update
Browse files Browse the repository at this point in the history
  • Loading branch information
vgalvoso committed Sep 17, 2022
1 parent 0b38d69 commit e174063
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 129 deletions.
150 changes: 89 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,52 @@
This is a simple PHP MVC framework without too much dependencies that you don't need.

## Supports:
1. PHP 5.3.1^
1. PHP 7^
2. MYSQL
3. MSSQL
4. SQLite

## Getting Started
1. Download the [latest release] or install via composer [composer create-project vgalvoso/spm].
2. Configure your database to app/Libraries/Database.php.
3. Rename the root folder with your project name.
2. Configure your database in app/Config/Database.php.
3. Make sure the project is located inside your web server's root directory.
4. Now you can create Controllers,Views and Models.

## Routing
Creating routes is easy in SPM.

Open index.php.
Open routes.php.

Add your new route inside the switch statement.
Add your new route.
```php
//index.php
switch($path){
case "home"://route name
new Home();//your controller
break;
default:
echo "<h1> Page not found </h1>";
break;
}
//routes.php
use App\Controller\Home;

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

To get data from POST request, add parameter to your controller function
To get data from POST request, use post() function
```php
//index.php
case "post"://route name
Home::samplePost($_POST);//add parameter to get POST data
break;
//routes.php
use App\Controller\Home;

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

To get data from GET request, add parameter to your controller function
To get data from GET request, use getI() function
```php
//index.php
case "get"://route name
Home::sampleGet($_GET);//add parameter to get GET data
break;
//routes.php
use App\Controller\Home;

//get(route name,class,static function to call)
get('get',Home::class,"sampleGet");
```
## Controllers
Controllers responds to hyperlinks,form actions and url inputs.

- Controllers are stored in app/Controller
- Controllers class name must be the same as the file name.
- Controllers extends app/Libraries/Controller.php

Lets create the function for the "post" route in Home controller.
```php
Expand All @@ -78,18 +74,26 @@ Views shows information to the user.

Let's use our view for Home controller(home route)
```php
$this->view("section/header");
$this->view("home");
$this->view("section/footer");
//app/Controller/Home.php
public static function index()
{
view("section/header");
view("home");
view("section/footer");
}
```

You can pass data to view. (Must be an associative array)
```php
$data = ["header" => "Simple MVC Framework",
"sub_header" => "Just what you need!"];
$this->view("section/header");
$this->view("home",$data);
$this->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");
}
```

Keys from the array will be converted into variables that you can use in the views
Expand All @@ -105,8 +109,6 @@ Models are used for CRUD(Create,Update,Delete) operations and SPM made that easy

- SPM currently supports Transactional Databases (MySql and MsSql).
- Models are stored in app/Model
- Model class names must be same as the file name.
- Model class names must be PascalCase and have Model at the end(sample: HomeModel)
- Model extends app/Libraries/Model.php

Initialize our model
Expand All @@ -117,7 +119,7 @@ When we don't place parameter, our model will use the default database.
$home_model = new HomeModel();
```

Use other database that is configured in app/Libraries/Database.php
Use other database that is configured in app/Config/Database.php
```php
//app/Controller/Home.php
$home_model = new HomeModel("ms");
Expand All @@ -131,67 +133,93 @@ $home_model = new HomeModel(null,$host,$user,$pass,$dbname,$driver(mysq;/mssql))

Select single item
```php
//returns object and false on error
$query = "SELECT * FROM table WHERE id = 1";
$item = $home_model->getItem($query);
//app/Model/HomeModel.php
public function getUser(){
$query = "SELECT * FROM users WHERE id = 1";
return $this->getItem($query);
}
```

Select multiple items
```php
//returns array and false on error
$query = "SELECT * FROM table";
$items = $home_model->getItems($query);
//app/Model/HomeModel.php
public function getAllUsers(){
$query = "SELECT * FROM users";
return $this->getItems($query);
}
```

//Cast each item to object for easier coding.
foreach($items as $item){
$item = (object)$item;
Anti SQL Injection
```php
//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);
}
```

To INSERT,UPDATE or DELETE data
To Insert data create an assoc array and use table field names as array keys
```php
//returns boolean
$query = "INSERT FROM table VALUES('1','Juan Dela Cruz')";
$result = $home_model->exec($query);
//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;
}
```

We can use parepared statement.
To Delete data use exec() function
```php
$params = ["id" => $value];
$item = $home_model->getItem("SELECT * FROM table WHERE id = :id",$params);
$delete = $home_model->exec("DELETE FROM table WHERE id = :id",$params);
//app/Model/HomeModel.php
public function deleteUser($userId){
$params = ["id" => $userId];
$query = "DELETE FROM users WHERE id = :id";
$this->exec($query,$params);
}
```

$params = ["id"=>$value,"name"=>$name];
$update = $home_model->("UPDATE table SET name=:name WHERE id = :id",$params);
To Update data use exec() function
```php
//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);
}
```

Start Transaction
```php
$home_model->startTrans();
//inside your model
$this->startTrans();
```

Commit
```php
$home_model->commit();
$this->commit();
```

Rollback
```php
$home_model->rollback();
$this->rollback();
```

Everytime we use our Model functions and it fails, we can get the error
```php
$home_model->getError();
$this->getError();
```

We can also see what is the database driver we are currently using.
```php
$home_model->getDriver();
$this->getDriver();
```

And we can also get the last inserted id
```php
$home_model->lastId();
$this->lastId();
```
[latest release]: https://github.com/vgalvoso/spm/releases
16 changes: 16 additions & 0 deletions app/Config/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace App\Config;

class Database {
public static function db(){
return [
"default" => array(
"server" => "localhost",
"user" => "root",
"pass" => "",
"dbname" => "pms_db",
"driver" => "mysql"
)
];
}
}
19 changes: 11 additions & 8 deletions app/Controller/Home.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<?php
class Home extends Controller{
namespace App\Controller;

public function __construct()
class Home{

public static function index()
{
$data = array("header" => "Simple PHP MVC Framework",
"sub_header" => "Just what you need!");
$this->view("section/header");
$this->view("home",$data);
$this->view("section/footer");
view("section/header");
view("home",$data);
view("section/footer");
}

public static function samplePost($post_data){
public static function samplePost($postData){
//now you can get values from POST request
var_dump($postData);
}

public static function sampleGet($get_data){
public static function sampleGet($getData){
//now you can get values from GET request
//fdsfsd
var_dump($getData);
}
}
20 changes: 0 additions & 20 deletions app/Libraries/Controller.php

This file was deleted.

10 changes: 0 additions & 10 deletions app/Libraries/Database.php

This file was deleted.

Loading

0 comments on commit e174063

Please sign in to comment.