Laravel receives the instance code from the front-end ajax, laravelajax

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.