开发者

How to route a url in PHP?

开发者 https://www.devze.com 2023-03-06 08:49 出处:网络
I\'m trying to implement url routes into my own mvc frame开发者_开发百科work and I like to find out the best way to do it. I\'m thinking three solutions.

I'm trying to implement url routes into my own mvc frame开发者_开发百科work and I like to find out the best way to do it. I'm thinking three solutions.

  1. Make a XML file and read it in my frontend controller then load the matching controller.
  2. Make a table that stores routes then execute a query in my frontend controller then load the matching controller.
  3. use either xml or table and then load routes into memcache then use it.

my concern for #1 and #2 is that I have to read a table or xml for the every access. my concern for #3 is that not all the hosting companies support memcache.

Any suggestions would be appreciated!

Added: I think I confused some people. By 'route', I'm actually talking about rewriting...like...I want to rewrite visitors to '/controller/action' when they visit '/hello'

Thanks


I would not use XML or tables for this. This will require additional resources for such (in comparison) easy operation. You should have a script which is loaded by mod_rewrite, it parses the URL, loads the proper controller and executes the action.


Hey I know this is a little late, but please check out my Routes class. I know you may not need it now, but hopefully it will still be useful to others.

With this you could easily do exactly what you need to with simple syntax and rules. All you need is to break down the parts of the returned URL (from a Routes::route() call) to calculate your controller and action method (and any possible parameters).

The reason this routing library doesn't do that for you is because you may not be in an MVC world when using it, but it's not that difficult to create. Because it's so low-level you could even create routes dynamically, say from a database table or memcache.


I think I can re-phase and even generalize the problem:

  • You want to create a representation for something (in this case URL routes) that are easily human readable (eg, XML);
  • You might also like that this representation can be easily computer-generated (eg, from a database table);
  • At run-time you don't want the solution to be slow: eg, parsing a large XML file, reading from disk, or fetching rows from a database.
  • You don't know what caching solution will be available in a production environment.

So you should aim to:

  • Perform the slow operations (reading from a database, parsing XML) as little as possible - perhaps in a compile or build phase, or "on first run".
  • Perform the route matching in a fast way: compile the rules directly into PHP code, and execute them as regular expressions or some such.
  • Cache the rule code as a php file and include it as regular code. APC is a php code cache that is commonly available in all production environments.

This would lead me to implement a solution with the following classes and methods:

Router::addRoute(pattern, controller) - adds a route Router::match(uri) - returns matching controller

You can store routes in whatever format you fancy (XML, Json, in a database), and generate a simple PHP include file to load routes quickly at runtime:

<?php
// compiled_routes.php
$router = new Router();
$router->addRoute('/', 'HomeController');
$router->addRoute('/widgets', 'WidgetsController');

tl;dr: separate the route rule-parsing from the route matching. Perform rule-parsing only once, and compile the result into PHP code which can be cached by APC.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消