ÔõÑùÔÚThinkPHP6ÖÐʹÓÃÓѺõÄURLµØµã£¿
Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬Ô½À´Ô½¶àµÄÍøÕ¾ÐèҪ˼Á¿ÓÅ»¯Óû§ÌåÑ飬ÆäÖÐÒ»¸ö·½Ãæ¾ÍÊÇÓѺõÄurlµØµã¡£thinkphp ÊÇÒ»¿îÓÅÒìµÄ php ¿ò¼Ü£¬¹ØÓÚ url µØµãµÄ´¦Öóͷ£Ò²ÌṩÁ˱ã½ÝµÄ½â¾ö¼Æ»®¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚ thinkphp6 ÖÐʹÓÃÓÑºÃµÄ url µØµã¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÏàʶÏ ThinkPHP6 ÖйØÓÚ·ÓɵÄÏà¹Ø¿´·¨¡£Â·ÓÉÊÇÖ¸½« URL ÇëÇóת·¢µ½Ö¸¶¨µÄ¿ØÖÆÆ÷ºÍÒªÁ죬ʵÏÖ²î±ðµÄÓªÒµÂß¼¡£ThinkPHP6 ¿ÉÒÔͨ¹ý¿ØÖÆÆ÷×¢½âµÄ·½·¨½ç˵·ÓÉÐÅÏ¢£¬ÕâÖÖ·½·¨ÔÚά»¤Àú³ÌÖÐÔ½·¢Àû±ã¡£
¹ØÓÚ URL µØµã£¬ÎÒÃdz£¼ûµÄÓÐÒÔÏÂÁ½ÖÖÐÎʽ£º
1.¹Å°åµÄURLµØµãÈ磺http://www.example.com/index.php/Controller/Action/id/1
2.α¾²Ì¬URLµØµãÈ磺http://www.example.com/Controller/Action/id/1
ÔÚµÚÒ»ÖֹŰåµÄ URL µØµãÖУ¬ÎÒÃÇÐèÒªÔÚ URL µØµãÖÐÊÖ¶¯Ìí¼Ó¿ØÖÆÆ÷ºÍÒªÁìÃû²¢ÒÔ id ²ÎÊýµÄ·½·¨×ª´ïÊý¾Ý£¬ÉÔÓÐʧÉ÷¿ÉÄܻᵼÖ´úÂëÔÓÂÒÇÒÄÑÒÔά»¤¡£ÔÚµÚ¶þÖÖα¾²Ì¬ URL µØµãÖУ¬ÎÒÃÇ¿ÉÒÔ½« ID µÈ²ÎÊýת»¯Îª¿É¶ÁÐÔ¸ßÇÒÒ×ÓÚά»¤µÄ URL µØµã¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
½ÓÏÂÀ´£¬ÎÒÃÇÏÈÈÝÔõÑùʹÓ÷ÓÉÀ´ÊµÏÖα¾²Ì¬ URL µØµã¡£
1.ÉèÖ÷ÓÉ
config/route.php ÎļþÊÇ·ÓÉÎļþ£¬ÎÒÃÇ¿ÉÒÔÔÚ´ËÎļþÖÐÉèÖ÷ÓÉÐÅÏ¢¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÉèÖÃʾÀý£º
use thinkacadeRoute; Route::rule('article/:id', 'index/article/read') ->pattern(['id' => 'd+']);
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ý Route::rule() ÒªÁìÉèÖÃÁËÒ»¸öÃûΪ article/:id µÄ URL µØµã£¬ÆäÖÐ :id ²¿·ÖÊÇ URL ²ÎÊý£¬ËüÌåÏÖÐèҪת´ïµÄÊý¾Ý¡£Â·ÓɵÄÏÖʵ²Ù×÷ÔòÊǽ«´Ë URL µØµãת·¢µ½Ò»¸öÃûΪ index/article/read µÄ¿ØÖÆÆ÷ºÍÒªÁìÖС£
ͬʱ£¬ÎÒÃÇ¿ÉÒÔʹÓà ->pattern() ÒªÁìÀ´ÏÞÖƲÎÊýÃûÌá£ÔÚÉÏÊöÀý×ÓÖУ¬ÎÒÃÇÏÞÖÆÁË :id ²ÎÊýµÄÖµÖ»ÄÜÊÇÊý×Ö¡£
2.½ç˵¿ØÖÆÆ÷
½ç˵ºÃ·ÓÉÐÅÏ¢ºó£¬ÎÒÃÇÐèÒªÔÚ¿ØÖÆÆ÷ÖÐʵÏÖ¶ÔÓ¦µÄÒªÁ죬À´´¦Öóͷ£ÇëÇó²¢·µ»ØÄÚÈÝ¡£ÔÚ¿ØÖÆÆ÷Îļþ£¨Î»ÓÚ app/controller Ŀ¼Ï£©ÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵һ¸öÃûΪ article µÄ¿ØÖÆÆ÷ÀàºÍÒ»¸öÃûΪ read µÄÒªÁì¡£
namespace appcontroller; use thinkRequest; class Article { public function read($id) { return 'ÕýÔÚÔĶÁÎÄÕ ' . $id; } }
µÇ¼ºó¸´ÖÆ
3.»á¼ûURL
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýµØµãÀ¸»á¼û×ðÁú¿Ê±ÍøÕ¾ÁË£¬ÀýÈçÊäÈëÒÔÏ URL µØµã£ºhttp://www.example.com/article/1 £¬¶ÔÓ¦¿ØÖÆÆ÷ÖÐµÄ /:id ²ÎÊýֵΪ 1£¬´Ó¶ø½øÈë³ÌÐò´¦Öóͷ£Á÷³Ì¡£Íê³É·ÓÉת·¢ºó£¬ÎÒÃǽ«ÇëÇóת´ï¸ø Article ¿ØÖÆÆ÷µÄ read ÒªÁìÀ´´¦Öóͷ£ÇëÇó¡£ÔÚÍê³É´¦Öóͷ£ºó£¬ÎÒÃǽ«·µ»ØÄÚÈÝÏÔʾÔÚÇ°¶ËÒ³ÃæÖУ¬ÊµÏÖÁËÓÑºÃµÄ URL µØµã¡£
±¾ÎÄÏÈÈÝÁËÔÚThinkPHP6ÖÐʵÏÖÓѺÃURLµØµãµÄÏêϸҪÁ졣ʹÓ÷ÓÉ¿ÉÒÔÓÐÓõØ×èÖ¹·±ËöµÄ URL µØµãÊäÈëºÍ´ó×ڵIJÎÊýµÄת´ï£¬ÈôúÂëÔ½·¢Ò×ÓÚά»¤£¬Ìá¸ßÁËÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖÐʹÓÃÓѺõÄURLµØµã£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡