Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a reusable router / dispatcher for PHP?
    primarykey
    data
    text
    <p>I'm using a simple framework that handles requests based on query parameters.</p> <pre class="lang-none prettyprint-override"><code>http://example.com/index.php?event=listPage http://example.com/index.php?event=itemView&amp;id=1234 </code></pre> <p>I want to put clean urls in front of this so you can access it this way:</p> <pre class="lang-none prettyprint-override"><code>http://example.com/list http://example.com/items/1234 </code></pre> <p>I know how routes and dispatching works, and I could write it myself. But I would rather take advantage of all the code out there that already solves this problem. Does anyone know of a generic library or class that provides this functionality, but will let me <strong>return whatever I want from a route match</strong>? Something like this.</p> <pre><code>$Router = new Router(); $Router-&gt;addRoute('/items/:id', 'itemView', array( 'eventName' =&gt; 'itemView' )); $Router-&gt;resolve( '/items/1234' ); // returns array( 'routeName' =&gt; 'itemView', // 'eventName' =&gt; 'itemView, // 'params' =&gt; array( 'id' =&gt; '1234' ) ) </code></pre> <p>Essentially I would be able to do the dispatching myself based on the values resolved from the path. I wouldn't mind lifting this out of a framework if it's not too much trouble (and as long as the license permits). But usually I find the routing/dispatching in frameworks to be just a little too integrated to repurpose like this. And my searches seem to suggest that people are writing this themselves if they're not using frameworks.</p> <p>A good solution would support the following:</p> <ul> <li>specify routes with colon notation or regex notation</li> <li>parse parameters out of routes and return them somehow</li> <li><p>support fast reverse lookup like so:</p> <pre><code>$Router-&gt;get( 'itemView', array( 'id' =&gt; '1234' ) ); // returns 'items/1234' </code></pre></li> </ul> <p>Any help is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload