Skip to content

Refactor routes logic splitting Web::class #8

@arshavinel

Description

@arshavinel

Description

Note: this task requires task #1 to be done.


Web::class has a bad design. It just has a bunch of static methods for requests and routes.


It should be splitted respecting the following:

  • Http\Request::class will receive http request data;

    • ex: new Request($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER).
  • Http\RouteCollection::class will store all routes;

    • ex: new RouteCollection(array).
  • Http\Route::class will be created by RouteCollection object;

    • ex: new Route(key, array);
    • ex: RouteCollection::getByRequest(Request).

Utility classes:

  • StaticHandler::class acts as a static handler for alll objects.

  • Http\URL::class should contains only utils static methods for manipulating urls.


config/forks/ must be renamed into config/routes/.


Note: this task is part of the milestone named Improve Request methods usage.


Example

For framework usage:

use Arshwell\Monolith\Http\Request;
use Arshwell\Monolith\Http\RouteCollection;
use Arshwell\Monolith\Http\Route;
use Arshwell\Monolith\StaticHandler;

$request = new Request($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
$routeCollection = new RouteCollection(array); // array of all routes
$route = $routeCollection->getByRequest($request); // returns Route|null

// static handler for global usage
StaticHandler::setRequest($request);
StaticHandler::setRouteCollection($routeCollection);
StaticHandler::setRoute($route);

For user usage:

use Arshwell\Monolith\Http\Request;
use Arshwell\Monolith\Http\Route;

$request = new Request(); // gets minimal default values

$request->setUrlPath("path/to/page");
$request->setMethod("POST");

$key = StaticHandler::getRouteCollection()->getKeyByRequest($request);
$route = StaticHandler::getRouteCollection()->getByRequest($request); // returns Route|null

$route->getUrl();
$route->generateRequest(); // returns Request

For html usage:

<a href="<?= Arshwell\Monolith\StaticHandler::getRoute()->getUrl() ?>">
    this page
</a>

<a href="<?= Arshwell\Monolith\StaticHandler::getRouteCollection()->getByKey('key')->getUrl() ?>">
    page nr 2
</a>

<a href="<?= Arshwell\Monolith\StaticHandler::getRouteCollection('key')->getUrl() ?>">
    page nr 2
</a>

Arshwell Version

0.*

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions