How to implement the URL pattern interpreter used by Django and RoR in PHP

What is the best way to implement a URL interpreter / manager, such as found in Django and RoR, in PHP?

It should be able to interpret the query string as follows:

  • /users/show/4 displays
    • area = Users
    • action = show
    • Id = 4
  • /contents/list/20/10 displays
    • area = Content
    • action = list
    • Start = 20
    • Count = 10
  • /toggle/projects/10/active displays
    • action = toggle
    • area = Projects
    • id = 10
    • field = active

If the query string can be a given GET / POST variable or a string passed to the interpreter.

: , mod_rewrite.

: URL-, URL-. Drupal mod_rewrite , http://host/node/5 http://host/?q=node/5. $_REQUEST ['q']. .

+3
7

, MVC.

, - - Zend Framework, CakePHP, Symfony, Code Ignitor, Kohana, Solar Akelos.

+2

cakephp :

https://trac.cakephp.org/browser/trunk/cake/1.2.x.x/cake/dispatcher.php

https://trac.cakephp.org/browser/trunk/cake/1.2.x.x/cake/libs/router.php

- mod_rewrite:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*)$ $1 [L]
    RewriteRule ^([a-z]{2})/(.*)$ $2?lang=$1 [QSA,L]
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

URL-, /en/foo /de/foo, index.php GET 'lang' amd 'url'. - "", "" ..

+2

mod_rewrite? RoR mod_rewrite. , Django , mod_php URL- , , , PHP URL- ( ), mod_rewrite URL-.

+2

, , URL-. PEAR Net_URL_Mapper. , , unit test.

+2

, , . wordpress.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

.htaccess , - 404, index.php.

/index.php "" URL-.
index.php - :

$req = $_SERVER['REQUEST_URI'];
$req = explode("/",$req);

URL "/".

$area = $req['0'];
$action= $req['1'];
$id = $req['2'];

:

function get_page($offset) {//offset is the chunk of URL we want to look at
    $req = $_SERVER['REQUEST_URI'];
    $req = explode("/",$req);
    $page = $req[$offset];
    return $page;
}
$area   = get_page(0);
$action = get_page(1);
$id     = get_page(2);  

, !

+1

@Cheekysoft Zend_Controller Zend Framework. Front Controller, ( MVC ).

, , CakePHP RoR.

0

PHP, , , - , Django, PHP, :

URL- RESTful, Django (http://example.com/do/this/and/that/), , , mod _ . , , URI, script (http://example.com/index.php/do/this/and/that/). mod _ rewrite , , mod _.

, GET (http://example.com/index.php?do/this/and/that/) , GET . , . $_SERVER - Apache. , phpinfo(), , .

, .

-2

Source: https://habr.com/ru/post/1698255/


All Articles