vueÏîÄ¿Ôõô¸úthinkphpÍŽá
vueÊÇÒ»ÖÖÏÖ´ú»¯µÄjavascript¿ò¼Ü£¬ÓÃÓÚ¹¹½¨»¥¶¯ÐÔÇ¿µÄwebÓ¦ÓóÌÐò¡£¶øthinkphpÔòÊǺ£ÄÚ×ÅÃûµÄphp¿ªÔ´¿ò¼Ü£¬¿ÉÒÔ×ÊÖú¿ª·¢Õß¿ìËÙ¹¹½¨¸ßЧ¡¢¸ßÖÊÁ¿µÄwebÓ¦ÓóÌÐò¡£ÔÚÏÖʵ¿ª·¢ÖУ¬vueºÍthinkphp¾³£±»Í¬Ê±Ê¹Óã¬Òò´Ë½«¶þÕßÍŽáÆðÀ´ÊǼ«Îª³£¼ûµÄÐèÇó¡£
±¾ÆªÎÄÕ½«ÏÈÈÝVueºÍThinkPHPµÄ»ù±¾¿´·¨¼°ÔõÑù½«¶þÕßÍŽáʹÓá£ÈôÊÇÄãÒѾÊìϤÁËVueºÍThinkPHPµÄ»ù±¾ÖªÊ¶£¬ÄÇô¿ÉÒÔÖ±½ÓÌøÖÁÒÔÏÂÕ½ڣº
VueÏîÄ¿ÓëThinkPHPÍŽᡪ¡ªÇ°ºó¶ËÊèɢģʽ
Ç°¶ËÔõÑùŲÓÃThinkPHP½Ó¿Ú
ThinkPHPÈçÄÇÀïÖýӿÚÇëÇó
×ܽá
VueÏîÄ¿ÓëThinkPHPÍŽᡪ¡ªÇ°ºó¶ËÊèɢģʽ
VueÊÇÒ»ÖÖÇ°¶Ë¿ò¼Ü£¬ÓÃÓÚ¹¹½¨Óû§½çÃæ¡£¶øThinkPHPÔòÊÇÒ»ÖÖºó¶Ë¿ò¼Ü£¬ÓÃÓÚ¹¹½¨WebÓ¦ÓóÌÐòµÄЧÀÍÆ÷¶Ë¡£Òò´Ë£¬Ç°¶ËVueÏîÄ¿ºÍºó¶ËThinkPHPÓ¦ÓóÌÐòµÄÍŽᣬͨ³£½ÓÄÉÇ°ºó¶ËÊèɢģʽ¡£
Ç°ºó¶ËÊèɢģʽµÄ»ù±¾Ë¼Ð÷ÊÇ£¬½«Ç°¶ËVueÏîÄ¿Óëºó¶ËThinkPHPÓ¦ÓóÌÐòÊèÉ¢¿ªÀ´£¬Á½Õßͨ¹ýAPI¾ÙÐÐͨѶ¡£VueÏîÄ¿ÈÏÕæÌṩÓû§½çÃæºÍ½»»¥Âß¼£¬ThinkPHPÓ¦ÓóÌÐòÔòÈÏÕæ´¦ÀíÊý¾Ý¡¢Âß¼¡¢È¨Ï޵Ⱥǫ́´¦Àí¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Ç°ºó¶ËÊèɢģʽµÄÓŵãÔÚÓÚ£¬¿ÉÒÔ½«Ç°ºó¶Ë¿ª·¢ÊÂÇé»®·Ö·Ö¸øרÃŵÄÖ°Ô±¡£Ç°¶Ë¿ª·¢Ö°Ô±¿ÉÒÔרעÓÚ¹¹½¨Óû§½çÃæºÍ½»»¥Âß¼£¬´Ëºó¶Ë¿ª·¢Ö°Ô±¿ÉÒÔרעÓÚ´¦ÀíÊý¾ÝºÍÂß¼ÎÊÌâ¡£ÕâÑù£¬¿ÉÒÔÌá¸ß¿ª·¢Ð§ÂÊ£¬Í¬Ê±Ò²¿ÉÒÔ±ãÓÚά»¤ºÍÀ©Õ¹¡£
ÔÚÏÖʵ¿ª·¢ÖУ¬Ç°ºó¶ËÊèɢģʽµÄÏêϸʵÏÖ·½·¨ÓжàÖÖ£¬ÏÂÃæÊÇÒ»ÖÖ½ÏÁ¿µä·¶µÄ¼Æ»®£º
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚЧÀÍÆ÷¶Ë½¨ÉèÒ»¸öThinkPHPÓ¦ÓóÌÐò£¬ÓÃÓÚ½ÓÊÜÇ°¶ËVueÏîÄ¿·¢Ë͵ÄÇëÇ󣬲¢¾ÙÐÐÏìÓ¦µÄ´¦Àí¡£¿ÉÒÔÔÚThinkPHPµÄ¿ØÖÆÆ÷ÖбàдÏìÓ¦µÄ´¦Àí´úÂë¡£
½Ó×Å£¬ÔÚÇ°¶ËVueÏîÄ¿ÖУ¬ÎÒÃÇÐèҪʹÓÃVue Resource»òAxiosµÈHTTPÇëÇó¿â£¬Ïòºó¶Ë·¢ËÍÇëÇ󣬲¢´¦ÀíÏìÓ¦Êý¾Ý¡£¿ÉÒÔÔÚVue×é¼þÖбàдÏìÓ¦µÄÇëÇóºÍ´¦Àí´úÂë¡£
×îºó£¬½«VueÏîÄ¿ºÍThinkPHPÓ¦ÓóÌÐò°²ÅÅÔÚ²î±ðµÄЧÀÍÆ÷ÉÏ£¬Í¨¹ýAPI¾ÙÐÐͨѶ£¬Ê¹Ç°ºó¶Ë½»»¥Íê³É¡£
Ç°¶ËÔõÑùŲÓÃThinkPHP½Ó¿Ú
ÔÚÇ°¶ËVueÏîÄ¿ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃVue Resource»òAxiosµÈHTTPÇëÇó¿â£¬Ïòºó¶ËThinkPHPÓ¦ÓóÌÐò·¢ËÍÇëÇó¡£
ÒÔAxiosΪÀý£¬ÎÒÃÇ¿ÉÒÔÔÚVue×é¼þÖбàдÈçÏ´úÂ룺
import axios from 'axios' export default { data () { return { message: '' } }, methods: { getMessage () { axios.get('/api/getMessage').then(response => { this.message = response.data.message }) } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇÒýÈëÁËAxios¿â£¬²¢ÔÚVue×é¼þÖнç˵ÁËÒ»¸öÒªÁìgetMessage¡£Õâ¸öÒªÁì·¢ËÍÒ»¸öGETÇëÇó/api/getMessageÖÁºó¶ËThinkPHPÓ¦ÓóÌÐò£¬»ñÈ¡·µ»ØµÄÊý¾Ý£¬È»ºó½«·µ»ØµÄÐÂÎÅ´æ´¢ÔÚ×é¼þµÄdataÖС£
ÐèҪעÖصÄÊÇ£¬/api²¿·ÖÔÚÏÖʵ¿ª·¢ÖпÉÄÜ»áÓÐËù²î±ð£¬Ïêϸƾ֤Äã×Ô¼ºµÄÏîÄ¿ÉèÖöø¶¨¡£¸Ã²¿·Öͨ³£ÓÃÓÚ±êʶAPIµÄÈë¿Ú£¬ÌåÏÖÕâÊÇÒ»¸öAPIÇëÇ󣬶ø²»ÊÇͨË×µÄÒ³ÃæÇëÇó¡£
ÀàËƵأ¬ÎÒÃÇÒ²¿ÉÒÔʹÓÃAxios·¢ËÍPOSTÇëÇó¡¢PUTÇëÇóµÈÆäËüÀàÐ͵ÄÇëÇó¡£ÏêϸҪÁìºÍ²ÎÊý¿ÉÒԲο¼AxiosÎĵµ¡£
ThinkPHPÈçÄÇÀïÖýӿÚÇëÇó
ÔÚºó¶ËThinkPHPÓ¦ÓóÌÐòÖУ¬ÎÒÃÇ¿ÉÒÔ±àд¿ØÖÆÆ÷À´´¦ÀíÇ°¶ËVueÏîÄ¿·¢Ë͵ÄÇëÇó¡£
Ê×ÏÈ£¬ÐèÒªÔÚ·ÓÉÎļþÖÐÉèÖÃAPI·ÓÉ£¬½«APIÇëÇóת·¢µ½ÏìÓ¦µÄ¿ØÖÆÆ÷¡£
use thinkRoute; Route::group('api', function () { Route::get('getMessage', 'api/MessageController/getMessage'); });
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËThinkPHPµÄ·Óɹ¦Ð§£¬½«GETÇëÇó/api/getMessageת·¢µ½MessageControllerµÄgetMessageÒªÁìÀï¡£ÎÒÃÇ»¹¿ÉÒÔÔÚ·ÓÉÎļþÖÐÉèÖÃÆäËüÀàÐ͵ÄÇëÇó£¬ÈçPOST¡¢PUTµÈÀàÐÍÇëÇóµÄ·ÓÉ¡£
½Ó×Å£¬ÔÚMessageControllerÖУ¬ÎÒÃÇ¿ÉÒÔ±àдgetMessageÒªÁìÀ´´¦ÀíÇ°¶ËVueÏîÄ¿·¢Ë͵ÄÇëÇó¡£ÏÂÃæÊÇÒ»¸öÀý×Ó£º
namespace apppicontroller; use thinkController; class MessageController extends Controller { public function getMessage() { $message = 'Hello, Vue! This is a message from ThinkPHP.'; return ['message' => $message]; } }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÃûΪMessageControllerµÄ¿ØÖÆÆ÷£¬½ç˵ÁËgetMessageÒªÁì¡£¸ÃÒªÁì·µ»ØÒ»¸öÊý×飬ÆäÖаüÀ¨ÁËÒ»¸öÃûΪmessageµÄÐÂÎÅ£¬¸ÃÐÂÎŽ«×÷ΪÏìÓ¦Êý¾Ý·¢ËÍÖÁÇ°¶Ë¡£
ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇó£¬ÔÚ¿ØÖÆÆ÷ÖбàдÏìÓ¦µÄÊý¾Ý´¦ÀíÂß¼¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ¶ÁÈ¡Êý¾Ý¿â¡¢²Ù×÷SessionµÈ£¬½«´¦ÀíЧ¹ûÒÔJSONÃûÌ÷µ»Ø¸øÇ°¶Ë¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑù½«Ç°¶ËVueÏîÄ¿ºÍºó¶ËThinkPHPÓ¦ÓóÌÐòÍŽáʹÓ㬽ÓÄÉÁËÇ°ºó¶ËÊèɢģʽ¡£
ÏêϸÀ´Ëµ£¬ÎÒÃÇÒÔAxiosΪÀý£¬ÑÝʾÁËÔõÑùÔÚÇ°¶ËVueÏîÄ¿ÖÐʹÓÃAxios·¢ËÍHTTPÇëÇóÖÁºó¶ËThinkPHPÓ¦ÓóÌÐò£¬²¢ÀֳɻñÈ¡Á˺ó¶Ë´¦ÀíºóµÄÏìÓ¦Êý¾Ý¡£
ÔÚºó¶ËThinkPHPÓ¦ÓóÌÐòÖУ¬ÎÒÃÇʹÓÃÁË·Óɹ¦Ð§ºÍ¿ØÖÆÆ÷£¬ÈÏÕæÎüÊպʹ¦ÀíÀ´×ÔÇ°¶ËVueÏîÄ¿·¢Ë͵ÄÇëÇ󣬲¢½«´¦ÀíЧ¹ûÒÔJSONÃûÌ÷µ»Ø¸øÇ°¶Ë¡£
ËäÈ»£¬²¢²»¾ÖÏÞÓÚ±¾ÎÄËùÏÈÈݵļƻ®£¬ÉÐÓÐÆäËüÐí¶àÖÖʵÏÖÇ°ºó¶ËÊèÉ¢µÄ¼Æ»®¡£Ï£ÍûÕâƪÎÄÕ¿ÉÒÔ×ÊÖúÄã¸üÇáËɵØʵÏÖVueÏîÄ¿ÓëThinkPHPµÄÍŽᡣ
ÒÔÉϾÍÊÇvueÏîÄ¿Ôõô¸úthinkphpÍŽáµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡