├── .gitignore ├── README.md ├── app ├── Controllers │ └── HomeController.php ├── Helpers │ ├── DB.php │ ├── Params.php │ ├── Route.php │ └── View.php ├── Models │ └── User.php ├── config.php └── routes.php ├── composer.json ├── database.sql ├── index.php └── views └── home └── welcome.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Introduction 2 | A very simple & lightweight raw PHP framework that focuses mainly the developers, who want to speed up their development process. It is developed with MVC structure and does not use any external packages. 3 | 4 | ## Requirements 5 | - Composer is required. Get composer from here: https://getcomposer.org/download/ 6 | 7 | ## Installation 8 | - Clone the repository, 9 | - Run this command in the project directory: 10 | ```composer dump-autoload``` 11 | - Change your configuration in ```app/config.php``` file 12 | 13 | ### Enjoy! 14 | -------------------------------------------------------------------------------- /app/Controllers/HomeController.php: -------------------------------------------------------------------------------- 1 | 'John', 18 | 'last_name' => 'Doe', 19 | 'email' => 'john@example.com', 20 | 'age' => 29, 21 | 'country' => 'USA' 22 | ], 23 | (object) [ 24 | 'first_name' => 'Mary', 25 | 'last_name' => 'Moe', 26 | 'email' => 'mary@example.com', 27 | 'age' => 32, 28 | 'country' => 'UK' 29 | ], 30 | (object) [ 31 | 'first_name' => 'Erik', 32 | 'last_name' => 'Nilsson', 33 | 'email' => 'erik@example.com', 34 | 'age' => 20, 35 | 'country' => 'Sweden' 36 | ], 37 | (object) [ 38 | 'first_name' => 'Joe', 39 | 'last_name' => 'Martenson', 40 | 'email' => 'joe@example.com', 41 | 'age' => 35, 42 | 'country' => 'Norway' 43 | ], 44 | (object) [ 45 | 'first_name' => 'Steve', 46 | 'last_name' => 'Carrel', 47 | 'email' => 'steve@example.com', 48 | 'age' => 25, 49 | 'country' => 'Australia' 50 | ] 51 | ); 52 | 53 | return View::render('home/welcome.php', [ 54 | 'users' => $users 55 | ]); 56 | } 57 | 58 | public function get_user() 59 | { 60 | $users = array(); 61 | $users[] = User::find(Params::get('id')); 62 | 63 | return View::render('home/welcome.php', [ 64 | 'users' => $users 65 | ]); 66 | } 67 | 68 | // public function create_user() 69 | // { 70 | // $user = (object) [ 71 | // 'first_name' => 'Myra', 72 | // 'last_name' => 'Dalton', 73 | // 'email' => 'myra@example.com4', 74 | // 'age' => 21, 75 | // 'country' => 'Sweden' 76 | // ]; 77 | 78 | // User::create($user); 79 | // } 80 | 81 | } -------------------------------------------------------------------------------- /app/Helpers/DB.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 30 | $conn->exec("set names utf8"); 31 | } catch(\PDOException $e) { 32 | echo "Connection error: " . $e->getMessage(); 33 | } 34 | 35 | self::$conn = $conn; 36 | return $conn; 37 | } 38 | 39 | /** 40 | * Set SQL query 41 | * 42 | * @param string $sql 43 | * @param mixed $data 44 | * @return self 45 | */ 46 | public static function query($sql, $data = null) 47 | { 48 | // Get Connection 49 | $db = self::getConnection(); 50 | 51 | // Prepare query statement 52 | $stmt = $db->prepare($sql); 53 | 54 | $params = array(); 55 | 56 | if ($data) { 57 | // Sanitize 58 | foreach ($data as $key => $value) { 59 | if ($value) { 60 | $params[$key] = htmlspecialchars(strip_tags($value)); 61 | } 62 | } 63 | } 64 | 65 | self::$statement = $stmt; 66 | self::$params = $params; 67 | 68 | return new static(); 69 | } 70 | 71 | /** 72 | * Run the SQL query 73 | * 74 | * @return mixed 75 | */ 76 | public static function run() 77 | { 78 | try { 79 | return self::$statement->execute(self::$params); 80 | } catch (\PDOException $e) { 81 | return $e->getMessage(); 82 | } 83 | } 84 | 85 | /** 86 | * Run the SQL query and 87 | * get all the result 88 | * 89 | * @return array 90 | */ 91 | public static function get() 92 | { 93 | // Execute query 94 | self::$statement->execute(self::$params); 95 | $total = self::$statement->rowCount(); 96 | 97 | // Check if more than 0 record found 98 | $result = array(); 99 | 100 | if ($total > 0) { 101 | while ($row = self::$statement->fetch(PDO::FETCH_ASSOC)) { 102 | array_push($result, (object) $row); 103 | } 104 | } 105 | return $result; 106 | } 107 | 108 | /** 109 | * Run the SQL query and 110 | * get the first result 111 | * 112 | * @return object 113 | */ 114 | public static function first() 115 | { 116 | // Execute query 117 | self::$statement->execute(self::$params); 118 | $total = self::$statement->rowCount(); 119 | 120 | // Check if more than 0 record found 121 | $result = array(); 122 | 123 | if ($total > 0) { 124 | while ($row = self::$statement->fetch(PDO::FETCH_ASSOC)) { 125 | array_push($result, (object) $row); 126 | } 127 | return $result[0]; 128 | } else { 129 | return null; 130 | } 131 | } 132 | 133 | /** 134 | * Get last inserted ID 135 | * 136 | * @return int 137 | */ 138 | public static function insertedId() 139 | { 140 | // Get Connection 141 | $db = self::$conn; 142 | 143 | return (int) $db->lastInsertId(); 144 | } 145 | } 146 | 147 | ?> -------------------------------------------------------------------------------- /app/Helpers/Params.php: -------------------------------------------------------------------------------- 1 | method = $method; 45 | $this->path = $path; 46 | $this->action = $action; 47 | } 48 | 49 | /** 50 | * Add GET requests to routes 51 | * 52 | * @param string $path 53 | * @param string $action 54 | * @return void 55 | */ 56 | public static function get($path, $action) 57 | { 58 | self::$routes[] = new Route('get', $path, $action); 59 | } 60 | 61 | /** 62 | * Add POST requests to routes 63 | * 64 | * @param string $path 65 | * @param string $action 66 | * @return void 67 | */ 68 | public static function post($path, $action) 69 | { 70 | self::$routes[] = new Route('post', $path, $action); 71 | } 72 | 73 | /** 74 | * Get the routes array 75 | * 76 | * @return array 77 | */ 78 | public static function getRoutes() 79 | { 80 | return self::$routes; 81 | } 82 | 83 | /** 84 | * Handle route to destinated controller function 85 | * 86 | * @param string $path 87 | * @return void 88 | */ 89 | public static function handle($path) 90 | { 91 | $desired_route = null; 92 | 93 | foreach (self::$routes as $route) { 94 | $pattern = $route->path; 95 | $pattern = str_replace('/', '\/', $pattern); 96 | 97 | $pattern = '/^' . $pattern . '$/i'; 98 | $pattern = preg_replace('/{[A-Za-z0-9]+}/', '([A-Za-z0-9]+)', $pattern); 99 | 100 | if (preg_match($pattern, $path, $match)) { 101 | $desired_route = $route; 102 | } 103 | } 104 | 105 | $url_parts = explode('/', $path); 106 | $route_parts = explode('/', $desired_route->path); 107 | 108 | foreach ($route_parts as $key => $value) { 109 | if (!empty($value)) { 110 | $value = str_replace('{', '', $value, $count1); 111 | $value = str_replace('}', '', $value, $count2); 112 | 113 | if ($count1 == 1 && $count2 == 1) { 114 | Params::set($value, $url_parts[$key]); 115 | } 116 | } 117 | } 118 | 119 | if ($desired_route) { 120 | if ($desired_route->method != strtolower($_SERVER['REQUEST_METHOD'])) { 121 | http_response_code(404); 122 | 123 | echo '
Some text that represents the site...
61 |Shows a list of users:
67 |# | 72 |Firstname | 73 |Lastname | 74 |Email Address | 75 |Age | 76 |Country | 77 |
---|---|---|---|---|---|
= $i ?> | 84 |= $user->first_name ?> | 85 |= $user->last_name ?> | 86 |= $user->email ?> | 87 |= $user->age ?> | 88 |= $user->country ?> | 89 |