How to implement a PHP Framework series article "4" URL routing management

Source: Internet
Author: User
Tags php framework
Access functions ($ACT) in the controller ($CTL) in the Business module ($APP) directly via the URL parameter

We support 3 modes of routing

Normal Mode

_a= $app, _u= $ctl. $act

The simplest way to focus on implementing the business $act function, no need to write extra code

Why does the parameter name need to be underlined before it is explained?

Easy Mode

_easy= $app. $tpl. $ctl. $act

_easy= $app. $ctl. $act

In web development, we typically output a front-end page in $act,

In easy mode, if the $act handler function is not implemented, the corresponding front-end template file is automatically found and displayed.

For simple presentation pages, this routing pattern is suitable for use

URL Rewrite mode (requires Nginx or Apache configuration)

Apache: ^rewrite[\.\/] (. *) $/index.php?_rewrite=$1 [R,QSA]

Nginx:rewrite ^/rewrite[\.\/] (. *) $/index.php?_rewrite=$1 last;

Rewrite. {$app}. {$ctl}. {$act}. {$params}.html

Or a more elegant directory-access approach

rewrite/{$app}/{$ctl}/{$act}/{$params}.html

Where $params is the optional Parameters section. parameter list after format UrlEncode

If you want to pass SP_UID=1&D=1.2&P=SB, then $params = SP_UID%3D1%26D%3D1.2%26P%3DS%2FB

or SP_UID/1/D/1.2/P/SB

This mode can be used in certain scenarios where the URL cannot have?& special characters

1. In order to be able to pass QQ OAUTH2 login verification, need to configure rewrite rules

rewrite.thirdlogin.index.qqcallback.sp_uid%3d1.php

2. Resource static

Rewrite.upload.index.out.uidm%3d310ef4b.png

3. Payment Callback

rewrite.pay.weixin.native2_notify.php

4. Open platform Authorization callback

rewrite/web/component/message/_app_id/xxxxxxx.php

Partial implementation Code

1

2

3

4

5

6

7

8

9

10

11

12

13

$a = (! empty ($_request [' _a ']) && is_string ($_request [' _a '])? $_request [' _a ']: ' Web ';

if (!preg_match ('/^[\w\. +$/', $a)) {

Exit (' Invalid _app name! ' . Htmlspecialchars ($a));

}

$GLOBALS [' _uct '] [' APP '] =! Empty ($a)? Strtolower ($a): ' Web ';

$u = (! empty ($_request [' _u ']) && is_string ($_request [' _u '])? $_request [' _u ']: ' Index.index ';

if (!preg_match ('/^[\w\. +$/', $u)) {

Exit (' Invalid _url name! ' . Htmlspecialchars ($u));

}

$u = Explode ('. ', $u, 2);

$GLOBALS [' _uct '] [' CTL '] =! Empty ($u [' 0 '])? Strtolower ($u [' 0 ']): ' Index ';

$GLOBALS [' _uct '] [' ACT '] =! Empty ($u [' 1 '])? Strtolower ($u [' 1 ']): ' Index ';

Easy mode

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

Easy mode direct access to template TPL

if (! empty ($_request [' _easy ']) && is_string ($_request [' _easy '])) {

$easy = Explode ('. ', $_request [' _easy ']);

Switch (count ($easy)) {

Case 4:

$_get [' _u '] = $_request [' _u '] = $easy [2]. '.' . $easy [3];

if (Preg_match ('/^[\w\. +$/', $easy [1])) {

$GLOBALS [' _uct '] [' TPL '] = $easy [1];

}

$_get [' _a '] = $_request [' _a '] = $easy [0];

break;

Case 3:

$_get [' _u '] = $_request [' _u '] = $easy [1]. '.' . $easy [2];

$_get [' _a '] = $_request [' _a '] = $easy [0];

break;

Case 2:

$_get [' _u '] = $_request [' _u '] = $easy [1];

$_get [' _a '] = $_request [' _a '] = $easy [0];

break;

Case 1:

$_get [' _a '] = $_request [' _a '] = $easy [0];

break;

Default:

Exit (' Invalid _easy param! ' . Htmlspecialchars ($_request [' _easy ']);

}

}

Rewrite mode

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

URL Rewrite mode

if (! empty ($_request [' _rewrite ']) && is_string ($_request [' _rewrite '])) {

1. Support for Apache rewrite mode, after the missing parameter condition

if (Stripos ($_server [' Server_software '], ' nginx ') = = = = False) {

$_request [' _rewrite '] = UrlDecode (substr ($_server [' query_string '], strlen (' _rewrite= ')));

}

2. Discard the suffix name in the _rewrite

$rewrite = substr ($_request [' _rewrite '], 0, Strrpos ($_request [' _rewrite '], '. '));

3. Support/As Delimiter

$sp = '. ';

for ($i = 0; $i < strlen ($rewrite); $i + +) {

if (In_array ($rewrite [$i], Array ('. ', '/'))) {

$SP = $rewrite [$i];

break;

}

}

$rewrite = Explode ($sp, $rewrite, 4);

The last 1 paragraphs are required suffix names

Switch (count ($rewrite)) {

Case 3:

Case 4: {

$_get [' _a '] = $_request [' _a '] = $rewrite [0];

$_get [' _u '] = $_request [' _u '] = $rewrite [1]. '.' . $rewrite [2];

if (! empty ($rewrite [3])) {

if (Strpos ($rewrite [3], '/')) {

$params = explode ('/', $rewrite [3]);

for ($i =0; $i +1< count ($params), $i +=2) {

$_request [UrlDecode ($params [$i])] = UrlDecode ($params [$i +1]);

}

}

else {

foreach (Explode (' & ', $rewrite [3]) as $p) {

List ($k, $v) = explode (' = ', $p, 2);

$_request [UrlDecode ($k)] = UrlDecode ($v);

}

}

}

break;

}

Case 2:

$_get [' _a '] = $_request [' _a '] = $rewrite [0];

$_get [' _u '] = $_request [' _u '] = $rewrite [1];

break;

Case 1:

$_get [' _a '] = $_request [' _a '] = $rewrite [0];

break;

Default:

break;

}

}

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.