Laravel接收前端ajax傳來的數(shù)據(jù)的實(shí)例代碼
最近有時間把公司的項(xiàng)目整理一下,并把遇到的問題解決了。那么今天也算個學(xué)習(xí)筆記吧!
最近在做一個筆記的項(xiàng)目,技術(shù)棧如下:vue.js + laravel + mongodb
首先不得不感嘆vue的神奇,項(xiàng)目昨晚之后我會對整個項(xiàng)目中用到的技術(shù)和踩過的坑進(jìn)行一個總結(jié),今天先記錄一個前端傳送數(shù)據(jù)給后端,laravel接收的例子。
前端ajax插件我沒有使用vue-resource,說實(shí)話,用他遇到了坑,所以使用了axios.js,很好用,而且比vue-resource還小。
來看前端代碼(省略vue邏輯部分):
axios.post('index.php/login',{ email:this.email, pass:this.pass }).then(function(res){ console.log(res) }).then(function(){ console.log(321) })
this.email和this.pass即為用戶填寫的表單數(shù)據(jù),點(diǎn)擊登錄即執(zhí)行這個方法(驗(yàn)證數(shù)據(jù)格式?jīng)]問題后)。
來看Laravel如何接收這兩個值:
我們在app文件夾下建立一個GUser.php的Model文件,內(nèi)容如下:
<?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; } }
再在app/Http/Controllers下建立一個GUserController.php文件,內(nèi)容如下:
<?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; } }
當(dāng)然啦,這里沒有用到pass的值,我在這里省略了登錄驗(yàn)證的邏輯。
然后在路由文件web.php里配置:
Route::any(‘/login','GUserController@login');
到此結(jié)束。以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Laravel 批量更新多條數(shù)據(jù)的示例
- laravel實(shí)現(xiàn)批量更新多條記錄的方法示例
- Laravel框架實(shí)現(xiàn)的批量刪除功能示例
- Laravel中批量賦值Mass-Assignment的真正含義詳解
- Laravel框架數(shù)據(jù)庫CURD操作、連貫操作總結(jié)
- PHP開發(fā)框架Laravel數(shù)據(jù)庫操作方法總結(jié)
- Laravel 5框架學(xué)習(xí)之向視圖傳送數(shù)據(jù)
- Laravel5.1數(shù)據(jù)庫連接、創(chuàng)建數(shù)據(jù)庫、創(chuàng)建model及創(chuàng)建控制器的方法
- Laravel 5框架學(xué)習(xí)之向視圖傳送數(shù)據(jù)(進(jìn)階篇)
- Laravel中數(shù)據(jù)遷移與數(shù)據(jù)填充的詳細(xì)步驟
- Laravel 的數(shù)據(jù)庫遷移的方法
- Laravel框架學(xué)習(xí)筆記之批量更新數(shù)據(jù)功能
相關(guān)文章
ThinkPHP利用PHPMailer實(shí)現(xiàn)郵件發(fā)送實(shí)現(xiàn)代碼
本文章介紹了關(guān)于在thinkphp中利用了phpmailer來實(shí)現(xiàn)郵件發(fā)送的詳細(xì)教程,有需要的朋友可以參考一下2013-09-09PHP實(shí)現(xiàn)微信公眾平臺音樂點(diǎn)播
首先說一下思路,微信提供了接口,只要數(shù)據(jù)格式滿足它所給的接口的XML格式即可以發(fā)送給關(guān)注者對應(yīng)的音樂2014-03-03