Laravel receives the instance code from the front-end ajax, laravelajax
Recently, I have had time to sort out the company's projects and solve the problems I encountered. So today is also a learning note!
Recently, I am working on a project with the following technical Stack: vue. js + laravel + mongodb
First of all, I have to lament the magic of vue. After the project last night, I will summarize the technologies and pitfalls used in the entire project. Today, I will first record a front-end to transfer data to the backend, laravel receiving example.
I didn't use vue-resource as the front-end ajax plug-in. To be honest, he encountered a pitfall, so he used axios. js, which is very useful and smaller than vue-resource.
Let's look at the front-end code (vue logic is omitted ):
axios.post('index.php/login',{ email:this.email, pass:this.pass }).then(function(res){ console.log(res) }).then(function(){ console.log(321) })
This. email and this. pass are the form data filled in by the user. Click login to execute this method (after verifying that the data format is correct ).
Let's see how Laravel receives these two values:
In the app folder, create a GUser. php Model file with the following content:
<?phpnamespace 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; }}
Create a GUserController. php file under app/Http/Controllers. The content is as follows:
<?phpnamespace 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; }}
Of course, the pass value is not used here. I omit the login verification logic here.
Then configure in the Routing File web. php:
Route::any(‘/login','GUserController@login');
This ends. The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.