thinkphpÇ°ºó¶ËÊèÉ¢ÔõôÉèÖÃ
Ëæ×ÅÇ°ºó¶ËÊèÉ¢ÊÖÒÕµÄÆÕ¼°£¬Ô½À´Ô½¶àµÄ¿ª·¢Õß×îÏÈʵÑ齫×Ô¼ºµÄÏîÄ¿´Ó¹Å°åµÄmvc¼Ü¹¹×ª»»ÎªÇ°ºó¶ËÊèÉ¢µÄ¼Ü¹¹¡£¶øthinkphp×÷Ϊһ¿î¿ªÔ´µÄphp¿ò¼Ü£¬Ò²Æð¾¢ÏìÓ¦ÕâÒ»ÊÖÒÕ³±Á÷¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖÃthinkphpµÄÇ°ºó¶ËÊèɢģʽ£¬×ÊÖú¿ª·¢ÕßÔÚÏîÄ¿ÖÐÓ¦ÓøÃÊÖÒÕ¡£
ÇéÐÎÉèÖÃ
ÔÚʹÓÃthinkphpʵÏÖÇ°ºó¶ËÊèɢ֮ǰ£¬ÐèÒªÏȾÙÐÐÇéÐÎÉèÖá£Ê×ÏÈ£¬ÔÚÏîÄ¿¸ùĿ¼Ï£¬Ê¹ÓÃcomposer×°ÖÃthinkphp¿ò¼Ü£¬ÊäÈëÒÔÏÂÏÂÁ
composer create-project topthink/think
È»ºó£¬ÔÚApache»òNginxÖÐÉèÖÃÐéÄâÖ÷»ú£¬½«ÏîÄ¿¸ùĿ¼ÉèÖÃΪÍøÕ¾¸ùĿ¼¡£Í¬Ê±£¬»¹ÐèÒªÔÚPHPÉèÖÃÎļþÖÐÆôÓÃrewriteÄ£¿é£¬Ê¹µÃthinkphpÄܹ»Õý³£Ê¹Ó÷Óɹ¦Ð§¡£
ÉèÖ÷ÓÉ
ÔÚthinkphpÖУ¬Â·ÓÉÉèÖÃÊÇÒªº¦°ì·¨Ö®Ò»£¬Ò²ÊÇÇ°ºó¶ËÊèÉ¢µÄÖ÷Òª×é³É²¿·Ö¡£ÔÚÏîĿĿ¼Ï£¬ÕÒµ½configĿ¼ÖеÄroute.phpÎļþ£¬½«Æä·¿ª¡£È»ºó£¬Æ¾Ö¤Ç°ºó¶ËÊèÉ¢µÄÐèÇ󣬾ÙÐÐÈçÏÂÉèÖãº
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
£¨1£©½«Ä¬ÈϵÄ·ÓÉÉèÖøÄΪ£º
‘/’ => ‘index/index/index’
ÕâÒ»¸Ä¶¯¿ÉÒÔʹµÃĬÈÏ»á¼ûµÄÒ³Ãæ±äΪǰ¶ËÒ³Ã棬¶ø·Çºó¶ËÒ³Ãæ¡£
£¨2£©ÔÚ·ÓÉÉèÖÃÖÐÌí¼ÓÇ°¶ËÒ³ÃæµÄ·ÓɹæÔò£¬ÀýÈ磺
‘@^/:path$’ => ‘index/index/index’
ÆäÖУ¬:pathÌåÏÖÇ°¶ËÒ³ÃæµÄ·¾¶¡£Í¨¹ýÕâÒ»¹æÔò£¬¿ÉÒÔʵÏÖÇ°¶Ë·ÓɵÄÌøת¡£
£¨3£©ÔÚ·ÓÉÉèÖÃÖÐÌí¼Ó½Ó¿ÚµÄ·ÓɹæÔò£¬ÀýÈ磺
‘@^api/:controller/:action$’ => ‘api/:controller/:action’
ÆäÖУ¬:controllerÌåÏÖ¿ØÖÆÆ÷Ãû£¬:actionÌåÏÖÒªÁìÃû¡£Í¨¹ýÕâÒ»¹æÔò£¬¿ÉÒÔʵÏÖÇ°ºó¶ËÊý¾ÝµÄ½»»¥¡£
ÉèÖÿØÖÆÆ÷
ÔÚthinkphpÖУ¬¿ØÖÆÆ÷ÊÇÇ°ºó¶ËÊèÉ¢µÄÇÅÁº£¬ÈÏÕæ´¦ÀíÇ°¶ËÇëÇó£¬Å²Óúó¶ËAPI£¬²¢·µ»ØÊý¾Ý¡£ÔÚconfigĿ¼Ï£¬½¨ÉèÒ»¸öapi.phpÎļþ£¬²¢Ìí¼ÓÒÔÏÂÉèÖãº
return [
'default_return_type' => 'json'
µÇ¼ºó¸´ÖÆ
];
ÕâÒ»ÉèÖÿÉÒÔʹµÃAPI½Ó¿ÚµÄĬÈÏ·µ»ØÀàÐÍΪJSONÃûÌá£
È»ºó£¬ÔÚÏîĿĿ¼Ï£¬½¨ÉèÒ»¸öapiĿ¼£¬ÔÚapiĿ¼Ï£¬½¨ÉèÒ»¸öv1Ŀ¼£¬×÷ΪAPI°æ±¾ºÅ¡£ÔÚv1Ŀ¼Ï£¬½¨ÉèÏìÓ¦µÄ¿ØÖÆÆ÷Îļþ£¬ÀýÈ磺
namespace apppi1controller;
use thinkController;
class UserController extends Controller{
public function index(){ $data = ['name' => 'Tom', 'age' => '18']; return json($data); }
µÇ¼ºó¸´ÖÆ
}
ÕâÒ»¿ØÖÆÆ÷ÖУ¬Ê¹Óþ²Ì¬Êý¾ÝÄ£Äâºó¶ËÊý¾Ý£¬²¢½«ÆäÒÔJSONÃûÌ÷µ»Ø¸øÇ°¶Ë¡£¿ª·¢Õß¿ÉÒÔƾ֤ÏÖʵÐèÇ󣬱àд²î±ðµÄ¿ØÖÆÆ÷£¬ÊµÏÖ²î±ðµÄÊý¾Ý½»»¥·½·¨¡£
ÉèÖÃÇ°¶ËÒ³Ãæ
ÔÚÇ°¶ËÒ³ÃæÖУ¬ÐèÒª¾ÙÐÐÈçÏÂÉèÖãº
£¨1£©ÔÚ¿ª·¢ÇéÐÎÖУ¬Ê¹ÓÃwebpackµÈ´ò°ü¹¤¾ß½«Ç°¶ËÏîÄ¿ÌìÉú¾²Ì¬Îļþ£¬²¢°²Åŵ½thinkphpÏîÄ¿µÄpublicĿ¼ÖС£
£¨2£©ÔÚÇ°¶Ë·ÓÉÌøתʱ£¬ÐèҪʹÓÃajaxµÈÊÖÒÕ£¬½«Ç°¶Ë·ÓÉת´ï¸øºó¶Ë½Ó¿Ú¡£
£¨3£©ÔÚÓëºó¶Ë½Ó¿Ú½»»¥Ê±£¬ÐèÒª×ñÕÕRESTful API¹æ·¶£¬Ê¹ÓÃGET¡¢POST¡¢PUTµÈ¶¯´Ê¶Ô×ÊÔ´¾ÙÐвÙ×÷¡£
²âÊÔ½Ó¿Ú
ÔÚËùÓÐÉèÖö¼Íê³Éºó£¬¿ÉÒÔ¾ÙÐнӿڲâÊÔ¡£¿ª·¢Õß¿ÉÒÔʹÓÃpostmanµÈ¹¤¾ß£¬Ä£ÄâÇ°¶Ë·¢ËÍÇëÇ󣬲¢Éó²é·µ»ØЧ¹û¡£ÈôÊÇ·µ»ØµÄÊÇÔ¤ÆÚµÄJSONÊý¾Ý£¬Ôò˵Ã÷Ç°ºó¶ËÊèÉ¢ÊÖÒÕÒѾÀÖ³ÉÓ¦Óá£
×ܽá
thinkphpÊÇÒ»¿î¹¦Ð§Ç¿Ê¢µÄPHP¿ò¼Ü£¬Ö§³ÖÇ°ºó¶ËÊèÉ¢ÊÖÒÕ¡£Í¨¹ý±¾ÎÄÖÐËùÏÈÈݵÄÉèÖÃÒªÁ죬¿ª·¢Õß¿ÉÒÔ½«×Ô¼ºµÄÏîĿת»»ÎªÇ°ºó¶ËÊèÉ¢µÄ¼Ü¹¹£¬Ìá¸ßÏîÄ¿µÄ¿Éά»¤ÐÔ¡¢¿ÉÀ©Õ¹ÐԺͿÉÖØÓÃÐÔ¡£Í¬Ê±£¬Ò²¿ÉÒÔÔ½·¢ÎÞаµØÓ¦¶ÔÊг¡¾ºÕùºÍÊÖÒÕÀå¸ï£¬Ìá¸ßÏîÄ¿µÄ¾ºÕùÁ¦ºÍ½µµÍ¿ª·¢±¾Ç®¡£
ÒÔÉϾÍÊÇthinkphpÇ°ºó¶ËÊèÉ¢ÔõôÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡