Addin a lot of changes

This commit is contained in:
2026-08-20 07:49:40 -03:00
parent 795062bb6d
commit 5b6d9553ac
5 changed files with 437 additions and 232 deletions
+35 -26
View File
@@ -76,9 +76,9 @@ RouteCollection::patch("/[i:id]", "\App\SubPlugin\Plugin\PluginController@updat
RouteCollection::delete("/[i:id]", "\App\SubPlugin\Plugin\PluginController@destroy");
```
#### Laravel Compatibility (#ToDo)
#### Laravel Compatibility
It would be nice to add routes similar to how Laravel Does, avoiding the usage of the whole ClassPath
Routes can be added with the Laravel way
```php
<?php
@@ -299,6 +299,37 @@ use Routes\RouteCollection as RouteCollection;
RouteCollection::get('*', function() { })->middlewareIgnore("auth");
```
## Extending functionality
This library allows you to change and customize some behaviors
### Content Parser
This librar has as default the return types for the Routes
* `string` (Raw print the content)
* `Array` (JSON Encode The Return)
* `stdClass|object|mixed` (JSON Encode The Return)
But you can extend this functionality to process a return type in a customizible way with the API `RouteCollection::addParser(string $type, callable $function)`
```php
<?php
use Routes\RouteCollection;
// When the returned object is the type Collection. This parser will be used
RouteCollection::addParser("Collection", function(Collection $collection): void{
header('Content-Type: application/json; charset=utf-8');
echo json_encode($collection->get());
});
```
### Object Spawners
The functions called by the Routing system might have a Data Type defined as a parameter. With the spawners, you can configure how this Objects will be created and passed prepared to the target function.
With the default Behavior, in order, we grab the function parameters, match with the variable segments of the URL and instanciate a object from the defined type passing the URI parameter as the __construct main parameter
## Server configuration
#### Requirements
@@ -424,33 +455,11 @@ server {
The package consist in 3 main files, with `RouteCollection.php` providing the main API
- `Route.php` => Stores each route information
- `RouteCollection.php` => Aggregate all routes and provide a interface to interact with the routes dataset
- `RouteGroup.php` => Aggregate a group of routes based on their namespaces
- `Route.php` => Stores each route information
The class `RouteCollection` is treated as a Singleton that return it's own global instance when loaded. The main interface would be something like: `RouteCollection::add($verb, $uri, $callback, $weight = 0)`, this method will return the instance of the `Route` object, so that the Route properties can be configured
*Route settings:*
- `RouteCollection::add($verb, $uri, $callback, $weight = 0)->doBlock()`
- After this route execute, no more routes would be executed
- `RouteCollection::add($verb, $uri, $callback, $weight = 0)->notBlock()`
- The execution of this route will not block the execution of the next routes
- `RouteCollection::add($verb, $uri, $callback, $weight = 0)->doIgnore()`
- This route will not count as a executed route, so a 404 can be detected
- `RouteCollection::add($verb, $uri, $callback, $weight = 0)->middlewareIgnore($name = '')`
- This route will not execute a registered global middleware
- `RouteCollection::add($verb, $uri, $callback, $weight = 0)->middlewareAdd($name = '', $params = [])`
- Register that this route must pass by a registered middleware
- `RouteCollection::add($verb, $uri, $callback, $weight = 0)->middlewareAppend($name = '', $function)`
- Append a ad-hock middleware to this specific route
- `RouteCollection::add($verb, $uri, $callback, $weight = 0)->setWeight($weigth = 0)`
- Define the priority of this route on the execution chain
- `RouteCollection::add($verb, $uri, $callback, $weight = 0)->setName($name)`
- Add a property name for the route object
- `RouteCollection::add($verb, $uri, $callback, $weight = 0)->setTag($key = '', $value = '')`
- Add a custom arbitrary tag on the route object
The class `RouteCollection` is treated as a Singleton that return it's own global instance when loaded. The main interface would be something like: `RouteCollection::add($verb, $uri, $callback, $weight = 0)`, this method will return the instance of the `Route` object, so that the Route properties can be chained
## Other resources
https://stackoverflow.com/questions/8054165/using-put-method-in-html-form