Match a path against the route table from app code (Route::getRoutes()->match())

Open
#860 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Start with the public RouteTable and RouteInfo API returned by Application::routes(); the issue names no source files or tests, so locate their definitions and existing routing tests first. Read the related #421 discussion to understand how handlers are expected to access the route table. Done means the table can distinguish a matched route, fallback, method-not-allowed, and not-found result, with the matcher accessible from handlers.

Written by the indexing model from the issue text.

Description

We're porting a production Laravel 13 (PHP) app to Laravel for Rust (laravel-rust 0.1.5). In a few places the PHP app asks the router which route a path would reach:

$route = Route::getRoutes()->match(Request::create($path)); // throws a 404/405 if nothing matches
$route->isFallback;
$route->gatherMiddleware();

We use it for three things:

  • Sitemap: an SEO record's path is listed only when a guest's GET reaches a real, ungated route, never the page fallback or a login redirect.
  • CMS page slugs: a slug is rejected when a route already answers that path, so a published page can't be shadowed.
  • Redirect warnings: the admin is warned when a managed redirect hides a live route.

What Laravel for Rust has today (0.1.5): routing.md and urls.md cover registering routes and building URLs (route!, url::to), and route:list lists them. Nothing matches a path against the route table. Application::routes() returns a RouteTable of RouteInfo { methods, uri, domain, handler, middleware }, but:

  • it isn't in the Markdown docs;
  • it has no matching method;
  • a handler can't reach the Application to read it (#421).

Our workaround: hand-kept lists of reserved paths, plus a test that reads app.routes() and fails when the lists drift. This only works for single-segment, parameterless routes. Multi-segment routes (/settings/profile) and parameterised ones can't be covered.

Proposal: give the route table a matcher, for example

fn matches(&self, method: Method, path: &str) -> Match // Route(&RouteInfo) | Fallback(&RouteInfo) | MethodNotAllowed | NotFound

The fallback should be reported distinctly, and the matcher should be reachable from handlers through whichever accessor #421 settles on.

Related: #421 (read access to the route table from a handler).

Dominant language
Rust
Stars
847
Forks
174
Avg merge
5h 40m
Merged PRs (30d)
167

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from laravel/rust-framework

All issues in laravel/rust-framework

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.