-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rewrite to add support for OOP callbacks and extended features. Update sample and README Signed-off-by: Igor Ilic <[email protected]> * Update README Signed-off-by: Igor Ilic <[email protected]> * Add dynamic routes support and update example Signed-off-by: Igor Ilic <[email protected]> * Update README and sample code Signed-off-by: Igor Ilic <[email protected]> * Update README Signed-off-by: Igor Ilic <[email protected]> * Add support for OOP middlewares Update doc blocks and sample code * Cleanup and refactor * Add tests * Add tests * Add CodeShip badge * Add scripts section to composer file * Update phpunit configuration * Update README.md * Update actions
- Loading branch information
Showing
14 changed files
with
514 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
.idea/ | ||
sample/logs | ||
vendor/ | ||
*.lock | ||
*.lock | ||
docs/ | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
|
||
namespace Gac\Routing\Exceptions; | ||
|
||
|
||
use Exception; | ||
|
||
class CallbackNotFound extends Exception | ||
{ | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,82 @@ | ||
# Custom routing class for PHP | ||
This class allows you to create static or dynamic routes. This class was inspired by [PHP Slim framework](https://www.slimframework.com/) | ||
# Routing library for PHP | ||
|
||
[![License](https://poser.pugx.org/gac/routing/license)](//packagist.org/packages/gac/routing) [![Total Downloads](https://poser.pugx.org/gac/routing/downloads)](//packagist.org/packages/gac/routing) | ||
This library allows you to create static or dynamic routes. This library was inspired by [PHP Slim framework](https://www.slimframework.com/) | ||
|
||
# Install via composer | ||
[![License](https://poser.pugx.org/gac/routing/license)](https://packagist.org/packages/gac/routing) [![Total Downloads](https://poser.pugx.org/gac/routing/downloads)](https://packagist.org/packages/gac/routing) | ||
|
||
` | ||
composer require gac/routing | ||
` | ||
|
||
# Manual install | ||
Download the `Routes.php` file and include it. | ||
|
||
# Example | ||
|
||
```php | ||
use Gac\Routing\Exceptions\RouteNotFoundException; | ||
use Gac\Routing\Routes; | ||
|
||
function verify_token(?string $token = "") { | ||
echo "Token: $token<br/>"; | ||
} | ||
## Install via composer | ||
|
||
# include_once "../Routes.php"; IF NOT USING composer | ||
|
||
try { | ||
$routes = new Routes(); | ||
```shell | ||
composer require gac/routing | ||
``` | ||
|
||
$routes->add('/', function () { | ||
echo "Welcome"; | ||
})->middleware([ | ||
["verify_token", "test"] | ||
]); | ||
## Manual install | ||
|
||
$routes->add('/test', function () { | ||
echo "Welcome to test route"; | ||
}); | ||
Download the latest release from the [Releases page](https://github.com/gigili/PHP-routing/releases). | ||
|
||
$routes->add('/test_middleware', function () { | ||
echo "This will call middleware function without passing the parameter"; | ||
})->middleware(["verify_token"]); | ||
Don't forget to add these include statements to your php files: | ||
|
||
$routes->route(); | ||
} catch (RouteNotFoundException $ex) { | ||
header("HTTP/1.1 404"); | ||
echo "Route not found"; | ||
} catch (Exception $ex) { | ||
die("API Error: {$ex->getMessage()}"); | ||
} | ||
```php | ||
include_once "./Exceptions/CallbackNotFound.php"; | ||
include_once "./Exceptions/RouteNotFoundException.php"; | ||
include_once "./Request.php"; | ||
include_once "./Routes.php"; | ||
``` | ||
|
||
## After install | ||
|
||
To use this class properly you will need to create a `.htaccess` file at the root of the project. | ||
To use this library properly you will need to create a `.htaccess` file at the root of the project. | ||
|
||
Example of the `.htaccess` file would look like this: | ||
|
||
``` | ||
```apacheconf | ||
RewriteEngine On | ||
RewriteBase / | ||
RewriteCond %{REQUEST_FILENAME} !-f | ||
RewriteCond %{REQUEST_FILENAME} !-d | ||
RewriteRule ^(.+)$ index.php?myUri=$1 [QSA,L] | ||
RewriteRule ^(.+)$ index.php [QSA,L] | ||
``` | ||
|
||
Do **NOT** change the `?myUri=$1` part in the `.htaccess` file as that will prevent the class from working. | ||
### Note | ||
|
||
If you've named your main file differently, replace `index.php` in the `.htaccess` file with that ever your main application file is. | ||
|
||
## Quick start | ||
|
||
Sample code to allow you to quickly start with your development. | ||
|
||
```php | ||
use Gac\Routing\Exceptions\CallbackNotFound; | ||
use Gac\Routing\Exceptions\RouteNotFoundException; | ||
use Gac\Routing\Request; | ||
use Gac\Routing\Routes; | ||
|
||
## Note ## | ||
When using middleware make sure the middleware function has benn declared before the Routes class import. | ||
include_once "vendor/autoload.php"; # IF YOU'RE USING composer | ||
|
||
$routes = new Routes(); | ||
try { | ||
$routes->add('/', function (Request $request) { | ||
$request | ||
->status(200, "OK") | ||
->send(["message" => "Welcome"]); | ||
}); | ||
|
||
$routes->route(); | ||
} catch (RouteNotFoundException $ex) { | ||
$routes->request->status(404, "Route not found")->send(["error" => ["message" => $ex->getMessage()]]); | ||
} catch (CallbackNotFound $ex) { | ||
$routes->request->status(404, "Callback method not found")->send(["error" => ["message" => $ex->getMessage()]]); | ||
} catch (Exception $ex) { | ||
$code = $ex->getCode() ?? 500; | ||
$routes->request->status($code)->send(["error" => ["message" => $ex->getMessage()]]); | ||
} | ||
``` | ||
|
||
# Features | ||
## Features | ||
|
||
* [x] Static routes | ||
* [x] Dynamic routes | ||
* [x] Middleware | ||
* [] Prefixing routes | ||
* [x] Middlewares | ||
* [x] Prefixing routes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
|
||
namespace Gac\Routing; | ||
|
||
|
||
class Request | ||
{ | ||
/** | ||
* @var array Request data | ||
*/ | ||
private array $data; | ||
|
||
/** | ||
* Request constructor. | ||
*/ | ||
public function __construct() { | ||
$input = json_decode(file_get_contents("php://input")) ?? []; | ||
$_REQUEST = array_merge($_REQUEST, (array)$input); | ||
$this->data = $_REQUEST; | ||
} | ||
|
||
/** | ||
* Returns a value for a specified body argument | ||
* | ||
* @param string $key Which request body argument to be returned | ||
* | ||
* @return mixed|null Body argument value or NULL if the argument doesn't exist | ||
*/ | ||
public function get(string $key = ""): mixed { | ||
return $this->data[$key] ?? NULL; | ||
} | ||
|
||
/** | ||
* Returns list of all the header items or a value of a specific item | ||
* | ||
* @param string $key Name of a specific item in the header list to return the value for | ||
* | ||
* @return array|string|null List of header values or a value of a single item | ||
*/ | ||
public function headers(string $key = ""): array|string|null { | ||
$headers = getallheaders(); | ||
return empty($key) ? $headers : $headers[$key] ?? NULL; | ||
} | ||
|
||
/** | ||
* Sets the header status code for the response | ||
* | ||
* @param int $statusCode Status code to be set for the response | ||
* @param string $message Message to be returned in the header alongside the status code | ||
* | ||
* @return Request Returns an instance of the Request class so that it can be chained on | ||
*/ | ||
public function status(int $statusCode = 200, string $message = ""): self { | ||
header("HTTP/1.1 {$statusCode} {$message}"); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Send response back | ||
* | ||
* @param string|array|object $output Value to be outputted as part of the response | ||
*/ | ||
public function send(string|array|object $output) { | ||
echo json_encode($output); | ||
} | ||
} |
Oops, something went wrong.