skipperbent / simple-php-router

Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
631 stars 116 forks source link

Add fetch method #713

Closed jurakin closed 1 month ago

jurakin commented 2 months ago

I've added a new method suitable for preflight requests in JavaScript to solve a problem I encountered. I needed to accept the OPTIONS method, but I didn't want to use something similar to:

SimpleRouter::options("/{everything}", function() {
    response()->httpCode(200);
})->where(["everything" => ".*"]);

This would return a status code of 200 also if the route doesn't exist. So I present to you SimpleRouter::fetch. This method accepts the GET, POST and OPTIONS methods, but in the case of the OPTIONS method, it will not call your callback and will return an empty page.

SimpleRouter::fetch('foo', function() {
    // ...
});
jurakin commented 1 month ago

Closed to change target branch and update code. A new pull request will be opened.