new project structure

This commit is contained in:
xinatorus 2020-04-20 21:49:30 +02:00
parent dec5a9083a
commit 74c6426500
9 changed files with 151 additions and 2 deletions

View File

@ -3,9 +3,9 @@
root = true
[*]
tab_width = 4
tab_width = 3
[*.{php,phpt,inc}]
[*.{php,phpt,inc,phtml}]
charset = utf-8
end_of_line = lf
indent_size = 2

View File

@ -135,3 +135,34 @@ Distributed under the MIT License. See `LICENSE` for more information.
Project Link: [https://github.com/GamerClassN7/Smart_Home/](https://github.com/GamerClassN7/Smart_Home/) <br> <br>
<sup><sub>Tags (ignore): ESP32 ESP12 ESP08 ESP01 ESP Smart Home Automation System PWA PHP HTML JS DHT11 DHT22
## Folder structure
- /app
- /controllers
- UserController.php
- /models
- UserModal.php
- /views
- /layouts
- default.phtml
- /templates
- /components
- /pages
- Bootstrap.php
- Db.php
- Routes.php
- /library
- /types
- Units.php
- /vendor
- Controller.php
- Db.php
- Router.php
- config
- config.php /
- public
- /css
- /images
- /js
- .htaccess
- index.php

6
app/Bootstrap.php Normal file
View File

@ -0,0 +1,6 @@
<?php
// import autoload
// import routes
require_once './Routes.php';

9
app/Routes.php Normal file
View File

@ -0,0 +1,9 @@
<?php
$router = new Router();
$router->setDefault(function(){
echo '404';
});
$router->run($_SERVER['REQUEST_METHOD'], '/'.$_GET['url']);

View File

0
library/Controller.php Normal file
View File

101
library/Router.php Normal file
View File

@ -0,0 +1,101 @@
<?php
/**
* Mini Router
* @author github.com/Xinatorus
* https://github.com/Xinatorus/MiniRouter
*/
class Router{
private $routes;
private $defaultFunction;
private $params;
private $function;
public function __construct(){
$this->routes = [];
$this->function = NULL;
$this->defaultFunction = NULL;
$this->params = [];
}
public function add($method, $pattern, $function){
$this->routes[] = [
'method' => $method,
'pattern' => $pattern,
'function' => $function,
];
}
public function any($pattern, $function){
$this->add(NULL, $pattern, $function);
}
public function get($pattern, $function){
$this->add('GET', $pattern, $function);
}
public function post($pattern, $function){
$this->add('POST', $pattern, $function);
}
public function put($pattern, $function){
$this->add('PUT', $pattern, $function);
}
public function patch($pattern, $function){
$this->add('PATCH', $pattern, $function);
}
public function delete($pattern, $function){
$this->add('DELETE', $pattern, $function);
}
public function setDefault($function){
$this->defaultFunction = $function;
}
public function run($method, $uri){
if(!$this->matchRoute($method, $uri)){
$this->function = $this->defaultFunction;
}
if($this->function !== NULL){
if(strpos($this->function, '@') !== false){
list($class, $function) = explode('@', $this->function);
$method = new ReflectionMethod($class, $function);
if($method->isStatic()){
call_user_func_array([$class, $function], $this->params);
}else{
call_user_func_array([new $class, $function], $this->params);
}
}else if(class_exists($this->function)){
new $this->function(...$this->params);
}else if (is_callable($this->function)) {
call_user_func_array($this->function, $this->params);
}
}
}
private function matchRoute($method, $uri){
foreach($this->routes as $route){
$patternRegex = $this->convertPatternToRegex($route['pattern']);
$params = [];
if(($route['method'] === NULL || $route['method'] === $method) && preg_match($patternRegex, $uri, $params)){
array_shift($params);
$this->params = $params;
$this->function = $route['function'];
return true;
}
}
return false;
}
private function convertPatternToRegex($pattern){
$regex = preg_replace('@{\w+}@', '([\w\d\-\.,]+)', $pattern);
if ( substr($regex, -1) === '/' ) {
$regex .= '?';
}
return '@^' . $regex . '$@';
}
}

0
public/.htaccess Normal file
View File

2
public/index.php Normal file
View File

@ -0,0 +1,2 @@
<?php
require_once __DIR__ . '/../app/Bootstrap.php';