Initial commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
# Kavalanche/Router
|
||||
Simple routing library for web applications.
|
||||
|
||||
# Usage
|
||||
|
||||
1. Require kavalanche/security
|
||||
```bash
|
||||
composer require kavalanche/security
|
||||
```
|
||||
|
||||
2. Create `Router` instance.
|
||||
```php
|
||||
$router = new \Kavalanche\Router\Router();
|
||||
```
|
||||
3. Add your routes.
|
||||
```php
|
||||
$router->get('/', function() {
|
||||
echo 'Start';
|
||||
});
|
||||
|
||||
$router->get('/post(\d+)', function($id) {
|
||||
echo 'Post ' . $id;
|
||||
});
|
||||
|
||||
$router->get('/test/index', [Kavalanche\Router\TestController::class, 'index']);
|
||||
|
||||
$controller = new Kavalanche\Router\TestController();
|
||||
|
||||
$router->get('/test/post/(\d+)', [$controller, 'post']);
|
||||
```
|
||||
|
||||
4. Invoke `dispatch` method.
|
||||
```php
|
||||
$router->dispatch();
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "kavalanche/router",
|
||||
"description": "Router component for web applications",
|
||||
"type": "library",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Wojtek",
|
||||
"email": "w.lk@wp.pl"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Kavalanche\\Router\\": "src/"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^8.4"
|
||||
}
|
||||
}
|
||||
Generated
+1787
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Kavalanche\Router;
|
||||
|
||||
/**
|
||||
* @author wojtek
|
||||
*/
|
||||
class Route {
|
||||
|
||||
public $method;
|
||||
public $path;
|
||||
public $callback;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Kavalanche\Router;
|
||||
|
||||
/**
|
||||
* @author wojtek
|
||||
*/
|
||||
class Router {
|
||||
|
||||
private $allowedMethods = [
|
||||
'get',
|
||||
'post'
|
||||
];
|
||||
private $routes = [];
|
||||
|
||||
public function request($method, $path, $callback) {
|
||||
$route = new Route();
|
||||
$route->method = $method;
|
||||
$route->path = $path;
|
||||
$route->callback = $callback;
|
||||
$this->routes[] = $route;
|
||||
}
|
||||
|
||||
public function get($path, $callback) {
|
||||
return $this->request('get', $path, $callback);
|
||||
}
|
||||
|
||||
public function post($path, $callback) {
|
||||
return $this->request('post', $path, $callback);
|
||||
}
|
||||
|
||||
public function dispatch() {
|
||||
$requestMethod = strtolower($_SERVER['REQUEST_METHOD']);
|
||||
$requestUri = parse_url($_SERVER['REQUEST_URI']);
|
||||
foreach ($this->routes as $route) {
|
||||
$path = str_replace('/', '\/', $route->path);
|
||||
$path = '/^' . $path . '\/?$/';
|
||||
if (in_array($requestMethod, $this->allowedMethods, true)) {
|
||||
if ($requestMethod === $route->method) {
|
||||
if (preg_match($path, $requestUri['path'], $params) === 1) {
|
||||
array_shift($params);
|
||||
call_user_func_array($route->callback, $params);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
http_response_code(404);
|
||||
throw new Exception('Route not found!');
|
||||
exit;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user