iOS 应用程序与 Laravel webapp 通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25747364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:04:29  来源:igfitidea点击:

iOS app communicate with Laravel webapp

phpiosobjective-claravel

提问by Jeffrey

I'm working on a project based on an iOS app (native) who use a webapp (Laravel framework) to communicate. For exemple, ios user should use Laravel login to use the application. The laravel part of the project is done and work good on a computer (login,register etc…) But now i'm thinking how will i communicate with my futur ios App and my webapp using laravel framework. I dont know any ways to do that, maybe i need a special framwork on my ios app ?

我正在开发一个基于 iOS 应用程序(本机)的项目,该应用程序使用 web 应用程序(Laravel 框架)进行通信。例如,ios 用户应该使用 Laravel 登录来使用该应用程序。该项目的 Laravel 部分已经完成并且在计算机上运行良好(登录、注册等...)但现在我在想我将如何使用 Laravel 框架与我的未来 ios 应用程序和我的 web 应用程序进行通信。我不知道有什么方法可以做到这一点,也许我的 ios 应用程序需要一个特殊的框架?

I have no idea, can you help me ?

我不知道,你能帮我吗?

Thanks in advance

提前致谢

采纳答案by c-griffin

This is a loaded question.... My personal preference is to set up a set of API controllers so you can control them independently and version them.

这是一个沉重的问题.... 我个人的偏好是设置一组 API 控制器,以便您可以独立控制它们并对其进行版本控制。

1) Create a sub-set of controllers @ /app/controllers/api/v1
2) Give them all a namespace of api/v1

1) 创建控制器的子集@ /app/controllers/api/v1
2) 给它们所有的命名空间api/v1

<?php namespace api\v1;

3) Import whatever classes you need into the new namespace

3) 将您需要的任何类导入新的命名空间

<?php namespace api\v1;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use Usage;
use Auth;

4) Install an oAuth2 package
5) Set up routes that generate and validate tokens and place your protected routes in a route group. (my example below.)

4) 安装oAuth2 包
5) 设置生成和验证令牌的路由,并将受保护的路由放在路由组中。(下面是我的例子。)

Route::group(['prefix' => 'api/v1', 'before' => 'apiErrors'], function()
{

    Route::post('accessToken', function()
    {

        return AuthorizationServer::performAccessTokenFlow();

    });

    Route::group(['before' => 'oauth|setUser'], function()
    {

        Route::resource('usages', 'api\v1\UsagesController');
        Route::resource('connections', 'api\v1\ConnectionsController');
        Route::resource('users', 'api\v1\UsersController');

    });

});

6) Set up your new api controllers to return data in a manner that a mobile app can use (JSON)

6) 设置新的 api 控制器以移动应用程序可以使用的方式返回数据 (JSON)

public function index()
{

    $usages = Usage::with('device.model.manufacturer')
                    ->where('user_id', Auth::user()->id)
                    ->get();

    return Response::json($usages, $this->responseCode, $this->accessControl);

}

回答by Jeffrey

thanks for your complete answer ! but i have done an simple API controller without oAuth2 package. My controller for the moment just return true or false if login is okay and it works good. here my code for other people...

感谢您的完整回答!但我做了一个没有 oAuth2 包的简单 API 控制器。如果登录正常并且运行良好,我的控制器目前只返回 true 或 false。这是我给其他人的代码......

public function trylogin() {


         if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password'))) || Auth::attempt(array('username'=>Input::get('username'), 'password'=>Input::get('password')))) {
            return Response::json(array('status' => 'OK'));
        } else {
            return Response::json(array('status' => 'FAIL'));
            }    
}

here my api routes

这是我的 api 路线

Route::resource('api/v1', 'ApiController');
Route::get('api/v1_hello', 'ApiController@sayhello');
Route::get('api/v1_login', 'ApiController@trylogin') 

what do you think about security managment ? i can make my own token system validation on ios ?

您如何看待安全管理?我可以在 ios 上进行我自己的令牌系统验证吗?

EDIT

编辑

i finally found a solution, here the function in my ApiController : you just need to send to your api from iOS the token generate by Facebook or Google connexion. and in my case add a network parameter.

我终于找到了一个解决方案,这里是我的 ApiController 中的函数:您只需要将 Facebook 或 Google 连接生成的令牌从 iOS 发送到您的 api。就我而言,添加一个网络参数。

 public function registerOrLoginFromSocialNetWorkV1(){      

    if (Input::get('email') && Input::get('sn_id')) {
        //sn = social network
        if (User::where('email','=', Input::get('email'))->count() != 0) {
            $user = User::where('email','=', Input::get('email'))->first();
             $user->fb_id = Input::get('sn_id');
             $user->save();
              //return 'email already used';

        }
        else{

            if (User::where('fb_id','=', Input::get('sn_id'))->count() == 0) {
                $user = new User;
                $user->firstname = Input::get('firstname');
                $user->lastname = Input::get('lastname');
                $user->username = Input::get('username');
                $user->email = Input::get('email');
                $user->fb_id = Input::get('sn_id');
                $user->fk_role = 3;
                $user->yearofbirth = Input::get('birthday');

                //$user->yearofbirth = substr($me['birthday'],6,9);

                if (Input::get('sex') == 'male') {
                    $user->sex = 1;
                }
                else{
                    $user->sex = 0;
                }


                $user->save();

                Userslog::log('api_register_social_network');

            }
            else{
                $user = User::where('fb_id','=', Input::get('sn_id'))->first();
                if (!$user->yearofbirth){
                     $user->yearofbirth = Input::get('birthday');
                     $user->save();

                }

            }

        }

            //dd($user);
            Auth::login($user);




            $follows = Follow::where('user_id','=',$user->id)->get();

            return Response::json(array('user' => $user,'follows' => $follows));
    }
    else{
         return 'error';

    }




}