How to redirect a url in PHP?

I am trying to inject URL routes into my own mvc framework and I like to find the best way to do this. I think of three solutions.

  • Make an XML file and read it in my interface controller, then download the appropriate controller.
  • Create a table in which the routes are stored, then execute the query in my interface controller, then load the appropriate controller.
  • use either xml or a table, and then load the routes into memcache, then use it.

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

Any suggestions would be appreciated!

Added: I think I confused some people. "Route", I'm actually talking about rewriting ... for example ... I want to rewrite visitors to "/ controller / action" when they visit "/ hello"

thank

+3
source share
3 answers

I would not use XML or tables for this. This will require additional resources for such (in comparison) simple work. You should have a script that loads mod_rewrite, it parses the URL, loads the appropriate controller, and performs the action.

+2
source

Hey, I know this is a little late, but please check out my class of routes . I know that you may not need it now, but I hope it will be useful to others.

, , . , , URL ( Routes::route()), ( ).

, , , MVC , . , , , memcache.

0

, :

  • - ( URL- ), (, XML);
  • , (, );
  • , : , XML , .
  • , .

, :

  • ( , XML), - , ".
  • : PHP- .
  • php . APC php-, .

:

Router:: addRoute (, ) - Router:: match (uri) -

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

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

tl; dr: separate route parsing from route matching. Only parse the rules once and compile the result into PHP code that APC can cache.

Hope this helps.

0
source

All Articles