小編給大家分享一下PHP+Laravel的使用示例,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
道外網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),道外網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為道外上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的道外做網(wǎng)站的公司定做!
本文只是零散的應(yīng)用教程,默認(rèn) Laravel 項(xiàng)目已經(jīng)安裝完成,并正常運(yùn)行;
在項(xiàng)目根目錄下運(yùn)行命令
php artisan make:controller TestController
創(chuàng)建成功會(huì)提示 Controller created successfully.
創(chuàng)建成功后會(huì)在 app/Http/Controllers/
目錄下生成 TestController.php
文件
在 TestController.php
文件中加入
public function index(){ return view('test');}public function testAjax(){ echo '請(qǐng)求成功了';die;}
在 resources/views
目錄中新建一個(gè)視圖文件 test.blade.php
文件中的內(nèi)容如下
打開(kāi)路由文件 routes/web.php
,默認(rèn)路由如下:
下方新增一條展示測(cè)試 Ajax 頁(yè)面的路由
Route::get('test', [TestController::class, 'index'])->name('test.index');
新增一條接收 Ajax 請(qǐng)求的路由
Route::post('test', [TestController::class, 'testAjax'])->name('test.ajax');
更多路由相關(guān)內(nèi)容請(qǐng)查看文檔 路由《Laravel 8 中文文檔》
打開(kāi) resources/views/welcome.blade.php
文件,找到大概 111 行的位置:
復(fù)制內(nèi)容,修改為需要的測(cè)試頁(yè)面入口
<a href="{{route('test.index')}}" class="ml-1 underline"> 測(cè)試入口</a>
保存后刷新頁(yè)面,就能看到測(cè)試入口了
點(diǎn)擊測(cè)試入口,進(jìn)入測(cè)試頁(yè)面,會(huì)看到以下內(nèi)容
將下載好的 jquery.min.js
放入 public/assets/
目錄下
修改 resources/views/test.blade.php
文件的內(nèi)容
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>Test Ajax</title> <script src="{{asset('assets/jquery.min.js')}}"></script></head><body> 返回的內(nèi)容:<p style="color: red" class="response-message"></p> <form method="post" action="{{route('test.ajax')}}"> {!! csrf_field() !!} 提交的內(nèi)容:<input type="text" name="text"> <span class="submit-btn">提交</span> </form></body><script> $('.submit-btn').click(function () { let url = $(this).closest('form').attr('action'); let formData = $(this).closest('form').serialize(); $.post(url,formData,function (response) { $('.response-message').text(response); }) })</script></html>
點(diǎn)擊測(cè)試頁(yè)面的 提交
可以看到控制器中 testAjax()
返回的內(nèi)容已經(jīng)顯示在頁(yè)面上
文件路徑 app/Http/Controllers/TestController.php
原內(nèi)容
修改后的內(nèi)容:
文件路徑 resources/views/test.blade.php
$('.submit-btn').click(function () { let url = $(this).closest('form').attr('action'); let formData = $(this).closest('form').serialize(); $.post(url,formData,function (response) { let responseData = response.data; let appendStr = '<span style="border: 1px solid blue">'+responseData.text+'</span>'; $('.response-message').empty().append(appendStr); })})
保存后在頁(yè)面輸入框中輸入內(nèi)容,點(diǎn)擊提交后即可看到最新內(nèi)容
看完了這篇文章,相信你對(duì)PHP+Laravel的使用示例有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章名稱:PHP+Laravel的使用示例
本文鏈接:http://www.rwnh.cn/article6/ghciog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、微信小程序、手機(jī)網(wǎng)站建設(shè)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)