Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I make the router work with a query string? #36

Open
kristiwr opened this issue Dec 2, 2021 · 4 comments
Open

How do I make the router work with a query string? #36

kristiwr opened this issue Dec 2, 2021 · 4 comments

Comments

@kristiwr
Copy link

kristiwr commented Dec 2, 2021

What code do I need for the router to recognize an optional query string? I specifically need a query string because it will be parsed by JavaScript on the page. I tried creating two routes, one with a query string parameter and one without. The one without the query string works fine, but when the query string is present the router doesn't match the route. I looked on the Router page in the documentation but couldn't find anything about this. Can you help?

@kristiwr
Copy link
Author

kristiwr commented Dec 2, 2021

My code in routes.php:

Router::get(
    "{type}-selector/",
    "App\Controllers\SelectorController@handle"
)->name(ROUTE_SELECTOR);

Router::get(
    "{type}-selector/{query}",
    "App\Controllers\SelectorController@handle"
)->name(ROUTE_SELECTOR_WITH_QUERY);

And from the view model that uses the route helper to generate a selector URL:

$more_url = route(ROUTE_SELECTOR_WITH_QUERY, [
    "type" => $this->type,
    "query" => "?" . http_build_query($this->specifications)
]);

(I tried removing the ? from the query param and hardcoding it into the route, but that didn't fix it.)

The route helper generates this URL. It's an absolute URL, but I've made it relative here for privacy reasons.

/connector-selector/?foo=foo&bar=bar&baz=baz/

The handle method signature in the selector controller looks like this:

public function handle(string $type, string $query = ""): TimberResponse

However, the router doesn't match the link that the route helper generates.

@kristiwr
Copy link
Author

kristiwr commented Dec 3, 2021

It seems like the issue arises from AltoRouter->match() via Rareloop\Router\Router->match(). Router is only passing the URI path to AltoRouter, and AltoRouter->match() appears to lack support for query strings.

Router line 161:
$altoRoute = $this->altoRouter->match($request->getUri()->getPath(), $request->getMethod());

@kristiwr
Copy link
Author

kristiwr commented Dec 3, 2021

I'm going to try converting the route to use Lumberjack-style parameters, and have the selector view model use them to generate a traditional query string to embed in a data attribute in the view. Then my JavaScript app could reference that attribute.

@kristiwr
Copy link
Author

kristiwr commented Dec 3, 2021

That worked! It would be a good idea to add a note to the documentation for the router and route helper saying that query strings are not supported - and perhaps even a check in the route helper that throws an exception if a param value is a query string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant