laravelÒì²½ÇëÇóʵÏÖ
ÔÚwebÓ¦ÓóÌÐòÖУ¬Òì²½ÇëÇóÊÇÒ»ÖÖÊ®·ÖÓÐÓõÄÊÖÒÕ¡£Ëüͨ¹ýÔÚºǫִ́ÐÐÇëÇó£¬×èÖ¹ÁËÇ°¶ËµØÆÚ´ýЧÀÍÆ÷ÏìÓ¦Ëùµ¼ÖµÄʱ¼äÆÌÕÅ£¬Í¬Ê±Ò²Ìá¸ßÁËÓû§ÌåÑé¡£ÔÚlaravel¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃһЩҪÁìÀ´ÊµÏÖÒì²½ÇëÇóµÄ¹¦Ð§¡£
Ò»¡¢Ê²Ã´ÊÇÒì²½ÇëÇó
Òì²½ÇëÇóÊÇÔÚ²»Ë¢ÐÂÕû¸öÒ³ÃæµÄÇéÐÎÏÂÏòЧÀÍÆ÷·¢ËÍÇëÇ󣬲¢ÔÚºǫ́´¦Öóͷ£ÇëÇóµÄÒ»ÖÖÊÖÒÕ¡£ÕâÖÖÊÖÒÕ¿ÉÒÔ±»Ó¦ÓÃÔÚ²î±ðµÄ³¡¾°µ±ÖУ¬ºÃ±È£ºµ±ÎÒÃÇÐèÒªÏÔʾij¸öʹÃüµÄ½ø¶ÈÌõ»òÊÇÐèÒªÖ§¸¶Ò³Ãæ×Ô¶¯Ë¢Ð£¬ÒÔ±ãÈÿͻ§¸ÐÊܵ½Æ丶¿îµÄÀֳɡ£
¶þ¡¢LaravelÖÐʹÓÃÒì²½ÇëÇóµÄÒªÁì
1.ʹÓÃjQueryÖеÄajaxÒªÁìÀ´ÊµÏÖÒì²½ÇëÇó¡£
ÔÚLaravelÖпÉÒÔÇáËɵØÓëjQueryÅäºÏʹÓÃÀ´ÊµÏÖÒì²½ÇëÇóµÄ¹¦Ð§¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ°취À´Íê³ÉÕâ¸öÀú³Ì£º
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÒýÈëjQuery¿âÎļþ¡£¿ÉÒÔÔÚ×ðÁú¿Ê±HTMLÄ£°åÖеÄÍ·²¿»òÊǶ¥²¿À´Íê³ÉÕâ¸ö°ì·¨£º
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª±àд·¢ËÍÒì²½ÇëÇóµÄ´úÂë¡£Õâ¸ö´úÂëƬ¶Ï¿ÉÒÔ±»°üÀ¨ÔÚÈκÎÊÂÎñ´¦Öóͷ£º¯ÊýÖУ¬ºÃ±ÈÓû§µã»÷ij¸ö°´Å¥ºó£º
$(document).ready(function(){ $('#btn-submit').click(function(e){ e.preventDefault(); $.ajax({ type: 'POST', url: '/payment', data: { 'amount': $('#amount').val(), 'payment_method': $('#payment_method').val(), '_token': $('input[name="_token"]').val() }, success: function(data){ console.log('success'); console.log(data); //ÔÚ´Ë´¦½«·µ»ØµÄdataÊý¾ÝչʾÔÚÇ°¶ËÒ³ÃæÉÏ }, error: function(XMLHttpRequest, textStatus, errorThrown) { console.log('error'); console.log(XMLHttpRequest); console.log(textStatus); console.log(errorThrown); } }); }); });
µÇ¼ºó¸´ÖÆ
2.ʹÓÃLaravelµÄÐÐÁй¦Ð§À´ÊµÏÖÒì²½ÇëÇó¡£
Laravelµ×²ã×Ô´øÁËÒ»¸öÐÐÁÐϵͳÓÃÓÚÔÚºǫִ́ÐÐʹÃü¡£Í¨¹ýʹÓÃÐÐÁÐϵͳ£¬ÎÒÃÇ¿ÉÒÔ×èÖ¹¿Í»§¶ËÆÚ´ýЧÀÍÆ÷ÏìӦʱ¼ä½Ï³¤µÄÎÊÌ⣬²¢ÇÒ»¹¿ÉÒÔÖÎÀíËùÓкǫ́ʹÃüµÄÖ´ÐУ¬ÒÔ±ã¸üºÃµØ¾ÙÐе÷ÀíºÍÓÅ»¯¡£
½ÓÏÂÀ´£¬ÎÒÃǽ«Ê¹ÓÃLaravelµÄÐÐÁÐϵͳÀ´½ç˵Òì²½ÇëÇóµÄ´¦Öóͷ£°ì·¨¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½ç˵һ¸öеÄʹÃüÀà¡£Õâ¸öÀàÐèÒª¼ÌÐøÔÉúµÄLaravelµÄʹÃüÀ࣬²¢ÊµÏÖ¸ÃÀàÖеÄhandle()ÒªÁì¡£»ù±¾Ä£°å¿ÉÒÔÈçÏÂËùʾ£º
namespace AppJobs; use IlluminateBusQueueable; use IlluminateContractsQueueShouldQueue; use IlluminateFoundationBusDispatchable; use IlluminateQueueInteractsWithQueue; use IlluminateQueueSerializesModels; class ProcessPayment implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $amount; protected $paymentMethod; /** * Create a new job instance. * * @return void */ public function __construct($amount, $paymentMethod) { $this->amount = $amount; $this->paymentMethod = $paymentMethod; } /** * Execute the job. * * @return void */ public function handle() { //ÔÚ´Ë´¦Ö´ÐÐÒì²½ÇëÇóËùÐèµÄËùÓÐÊÂÇ飬ºÃ±ÈHTTPÇëÇó»òµç×ÓÓʼþ·¢ËÍ¡£ //´¦Öóͷ£ÇëÇóÍê±Ïºó½«Ð§¹ûÉúÑÄÔÚÊý¾Ý¿âÖУ¬¹©Ç°¶ËÒ³Ãæ¶ÁÈ¡¡£ } }
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔ½«Òì²½ÇëÇóµÄ´¦Öóͷ£Âß¼·ÅÔÚhandle()ÒªÁìÖоÙÐС£Í¨¹ýÔÚºó¶ËÖ´ÐÐÕâ¸öʹÃü£¬¿ÉÒÔ×èÖ¹Ò³Ã濨¶Ùµ¼ÖÂÓû§ÌåÑé±ä²îµÄÎÊÌ⣬ͬʱҲ²»»áÕû¸öÇëÇ󱻺ó¶ËµÄÖ´ÐÐʱ¼äËùÍÏÂý¡£
×îºó£¬ÎÒÃÇ¿ÉÒÔÔÚij¸ö¿ØÖÆÆ÷ÒªÁìÖд¥·¢Õâ¸öʹÃü£¬´Ó¶øʵÏÖÒì²½ÇëÇóµÄ´¦Öóͷ£¡£ºÃ±È£º
public function submitPayment(Request $request) { $amount = $request->input('amount'); $method = $request->input('payment_method'); ProcessPayment::dispatch($amount, $method); //´¥·¢Òì²½ÇëÇóʹÃü return response()->json(['message' => 'success']); }
µÇ¼ºó¸´ÖÆ
Èý¡¢×ܽá
Òì²½ÇëÇóÔÚWebÓ¦ÓóÌÐòµÄ¿ª·¢ÖÐÊÎÑÝ×ÅÊ®·ÖÖ÷ÒªµÄ½ÇÉ«¡£Ê¹ÓÃÒì²½ÇëÇóÊÖÒÕ£¬Äܹ»ÈÃ×ðÁú¿Ê±Ó¦ÓóÌÐòÔËÐеøü¿ì¸üÁ÷ͨ£¬´Ó¶øÌá¸ßÓû§µÄÌåÑé¡£ÔÚLaravel¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃjQueryµÄajaxÒªÁì»òÕßÊÇLaravelµÄÐÐÁÐϵͳÀ´ÊµÏÖÒì²½ÇëÇó¹¦Ð§¡£ÎÞÂÛÄãʹÓÃÄÄÖÖÒªÁ죬¶¼ÐèÒªÇмÇÔÚÐÄ£ºÒì²½ÇëÇóʹӦÓóÌÐò¸ü¿ì£¬¸ßЧ£¬¸ü¾ßÓÐÓû§ÌåÑ顣ϣÍû±¾ÆªÎÄÕÂÄܹ»¶ÔÄãÔÚʵÏÖÒì²½ÇëÇóʱÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇlaravelÒì²½ÇëÇóʵÏÖµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡