Skip to content

Commit

Permalink
Adding support for query based routing.
Browse files Browse the repository at this point in the history
  • Loading branch information
incraigulous committed May 2, 2024
1 parent 4062949 commit de5fefa
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/Middleware/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace CodeZone\Router\Middleware;

use CodeZone\Router\FastRoute\Routes;
use CodeZone\Router;
use CodeZone\Router\FastRoute\Routes;
use FastRoute;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -55,29 +55,32 @@ public function handle(Request $request, Response $response, $next)
{
$http_method = $request->getMethod();
$uri = $request->getRequestUri();
$routable_param_keys = apply_filters(namespace_string('routable_params'), [
'page',
'action',
'tab'
]) ?? [];
$routable_params = collect($request->query->all())->only($routable_param_keys);

// Strip query string (?foo=bar) and decode URI
$pos = strpos($uri, '?');
if ($pos !== false) {
$uri = substr($uri, 0, $pos);
}
$route_param = container()->make(Router::class)->config['route_param'] ?? null;

//Allow for including certain params in the route,
//Like page=general
//or action=save
//or tab=general
if (count($routable_params)) {
$uri = $uri . '?' . http_build_query($routable_params->toArray());
if ($route_param && $request->has($route_param)) {
$uri = $request->get($route_param);
} else {
$routable_param_keys = apply_filters(namespace_string('routable_params'), [
'page',
'action',
'tab'
]) ?? [];
$routable_params = collect($request->query->all())->only($routable_param_keys);
// Strip query string (?foo=bar) and decode URI
$pos = strpos($uri, '?');
if ($pos !== false) {
$uri = substr($uri, 0, $pos);
}
//Allow for including certain params in the route,
//Like page=general
//or action=save
//or tab=general
if (count($routable_params)) {
$uri = $uri . '?' . http_build_query($routable_params->toArray());
}
$uri = trim(rawurldecode($uri), '/');
}

$uri = trim(rawurldecode($uri), '/');

//Get the matching route data from the router
$dispatcher = container()->make(Router::class)->routes(function (Routes $r) {
if ($this->handler) {
Expand Down

0 comments on commit de5fefa

Please sign in to comment.