Created API for API requests

This commit is contained in:
2017-10-09 11:05:58 +07:00
parent 96e262d8ff
commit 6040809710
6 changed files with 307 additions and 42 deletions

View File

@@ -3,7 +3,7 @@ namespace Core;
class Router
{
private
protected
$routes = [],
$params = [];
@@ -32,6 +32,7 @@ class Router
{
$url = htmlspecialchars($url);
$url = substr_replace($url, '', 0, 1);
$url = rtrim($url, '/');
foreach ($this->routes as $route => $params) {
if (preg_match($route, $url, $matches)) {
@@ -106,17 +107,17 @@ class Router
return $url = $parts[0];
}
private function convertToStudlyCaps($string)
protected function convertToStudlyCaps($string)
{
return str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
}
private function getNamespace($controller)
protected function getNamespace($controller)
{
return $namespace = 'App\Controllers\\' . $controller;
}
private function convertToCamelCaps($string)
protected function convertToCamelCaps($string)
{
return lcfirst($this->convertToStudlyCaps($string));
}

24
Core/RouterApi.php Normal file
View File

@@ -0,0 +1,24 @@
<?php
namespace Core;
use App\Controllers\Api;
class RouterApi extends Router
{
public function dispatchApi($url)
{
$object = new Api();
if (isset($this->params['action'])) {
$action = $this->params['action'];
return $object->$action();
} else {
$method = $_SERVER['REQUEST_METHOD'];
$model = $this->params['model'];
return (isset($this->params['id'])) ?
$object->$method($model, $this->params['id'])
:
$object->$method($model);
}
}
}