《PHP學(xué)習(xí):Laravel接收前端ajax傳來的數(shù)據(jù)的實(shí)例代碼》要點(diǎn):
本文介紹了PHP學(xué)習(xí):Laravel接收前端ajax傳來的數(shù)據(jù)的實(shí)例代碼,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP應(yīng)用最近有時(shí)間把公司的項(xiàng)目整理一下,并把遇到的問題解決了.那么今天也算個(gè)學(xué)習(xí)筆記吧!
PHP應(yīng)用最近在做一個(gè)筆記的項(xiàng)目,技術(shù)棧如下:vue.js + laravel + mongodb
PHP應(yīng)用首先不得不感嘆vue的神奇,項(xiàng)目昨晚之后我會(huì)對(duì)整個(gè)項(xiàng)目中用到的技術(shù)和踩過的坑進(jìn)行一個(gè)總結(jié),今天先記錄一個(gè)前端傳送數(shù)據(jù)給后端,laravel接收的例子.
PHP應(yīng)用前端ajax插件我沒有使用vue-resource,說實(shí)話,用他遇到了坑,所以使用了axios.js,很好用,而且比vue-resource還小.
PHP應(yīng)用來看前端代碼(省略vue邏輯部分):
PHP應(yīng)用
axios.post('index.php/login',{
email:this.email,
pass:this.pass
}).then(function(res){
console.log(res)
}).then(function(){
console.log(321)
})
PHP應(yīng)用this.email和this.pass即為用戶填寫的表單數(shù)據(jù),點(diǎn)擊登錄即執(zhí)行這個(gè)方法(驗(yàn)證數(shù)據(jù)格式?jīng)]問題后).
PHP應(yīng)用來看Laravel如何接收這兩個(gè)值:
PHP應(yīng)用我們?cè)赼pp文件夾下建立一個(gè)GUser.php的Model文件,內(nèi)容如下:
PHP應(yīng)用
<?php
namespace App;
use Mongodb;
use DB;
class GUser extends Mongodb {
public static function login($email) {
$mongo = DB::connection('mongodb');
$res = $mongo->collection('user')->where('email',$email)->first();
return $res;
}
}
PHP應(yīng)用再在app/Http/Controllers下建立一個(gè)GUserController.php文件,內(nèi)容如下:
PHP應(yīng)用
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\GUser;
use Illuminate\Http\Request;
class GUserController extends Controller{
protected function login(Request $request) {
$email = $request->input('email');
$pass = $request->input('pass');
$res = GUser::login($email);
return $res;
}
}
PHP應(yīng)用當(dāng)然啦,這里沒有用到pass的值,我在這里省略了登錄驗(yàn)證的邏輯.
PHP應(yīng)用然后在路由文件web.php里配置:
PHP應(yīng)用
Route::any(‘/login','GUserController@login');
PHP應(yīng)用到此結(jié)束.以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/464.html