H_MZ 2015-10-10 20:56 采纳率: 0%
浏览 25

在Laravel中制作API

I am a laravel beginner. I am building a PHP backend using laravel that will serve multiple platforms. I want to make an api that multiple platforms can use. This is what I am thinking of doing:

my api can be accessed like this: URL.com/api/v1/login (this responds in JSON)

and , if it a web version, login can be accessed from URL.com/login.This should display a form which will send an AJAX request to

URL.com/api/v1/login

I am thinking of setting up different routes for this. something like :

routes::post(api/v1/login,"whatever controller")

and

routes::get(login,"login_controller_which_will_send_ajax_requests_to the_above_controller")

I would like to ask something like this is recommended. if not, what is a better way to implement this

  • 写回答

2条回答 默认 最新

  • 10.24 2015-10-10 21:38
    关注

    Suppose the multiple platforms you mentioned could be grouped as the website and mobile apps.

    The recommendation is to build them separately: build the website like what you would do traditionally, and another api merely for mobile apps.

    The reason is simple: For the website, you can use sessions to store user login status, but for mobile app, you need to use something like json web token to verify the user. It is going to be messy and hard to maintain if they share routes and actions.

    Of course there are exceptions. If you built website as a web app using some JS framework like AngularJS, you may just build the api and serve them all.

    评论

报告相同问题?