How to implement a php framework article series [4] url routing management-php Tutorial

Source: Internet
Author: User
How to implement a php framework article series [4] url routing management directly accesses functions ($ act) in the controller ($ ctl) of the business module ($ app) through url parameters)

We support three routing modes

Normal mode

_ A = $ app, _ u = $ ctl. $ act

The simplest way is to focus on implementing the business $ act function without writing additional code.

Why do we need to underline the parameter name?

Easy Mode

_ Easy = $ app. $ tpl. $ ctl. $ act

_ Easy = $ app. $ ctl. $ act

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

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

This routing mode is suitable for simple display pages.

Url rewriting mode (nginx or apache configuration is required)

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

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

Rewrite.{$app}.{$ctl).{actact).{params).html

Or more elegant Directory-based access

Rewrite/{$ app}/{$ ctl}/{$ act}/commandid params0000.html

$ Params is the optional parameter. list of parameters in urlencode format

To pass sp_uid = 1 & d = 1.2 & p = sb, $ params = sp_uid % 3D1% 26d % 3D1. 2% 26 p % 3Ds % 2Fb

Or sp_uid/1/d/1.2/p/sb

Which of the following cannot be included in some required URLs? & This mode can be used for special characters

1. in order to pass the qq oau2's login verification, you need to configure rewrite rules

Rewrite. thirdlogin. index. qqcallback. sp_uid % 3D1. php

2. static resources

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

3. payment callback

Rewrite. pay. weixin. native2_policy.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 \.] + $/', $ )){

Exit ('invalid_app name! '. Htmlspecialchars ($ ));

}

$ GLOBALS ['_ UCT'] ['app'] =! Empty ($ )? 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'] ['AC'] =! 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

21

22

23

24

25

26

// Directly access the template tpl in easy mode

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

21

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 rewriting mode

If (! Empty ($ _ REQUEST ['_ rewrite']) & is_string ($ _ REQUEST [' _ rewrite']) {

// 1. support for apache rewrite mode? Missing parameters

If (stripos ($ _ SERVER ['server _ soft'], 'nginx') === false ){

$ _ REQUEST ['_ rewrite'] = urldecode (substr ($ _ SERVER ['query _ string'], strlen (' _ rewrite = ')));

}

// 2. suffix in discard _ rewrite

$ Rewrite = substr ($ _ REQUEST ['_ rewrite'], 0, strrpos ($ _ REQUEST [' _ rewrite'], '.');

// 3. support/as the separator

$ 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 segment is a required suffix

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.