iOS应用程序与Laravel网络应用程序通信


iOS app communicate with Laravel webapp

我正在做一个基于iOS应用程序(本机)的项目,该应用程序使用Web应用程序(Laravel框架)进行通信。例如,ios用户应该使用Laravel登录来使用该应用程序。项目的主要部分已经完成,并且在计算机上运行良好(登录、注册等)但现在我在想,我将如何使用laravel框架与我未来的ios应用程序和网络应用程序进行通信。我不知道怎么做,也许我需要在我的ios应用程序上做一个特殊的框架?

我不知道,你能帮我吗?

提前感谢

这是一个已加载的问题。。。。我个人的偏好是设置一组API控制器,这样你就可以独立控制它们并对它们进行版本控制。

1) 创建控制器子集@/app/controllers/api/v1
2) 给他们一个api/v1 的名称空间

<?php namespace api'v1;

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

<?php namespace api'v1;
use Illuminate'Support'Facades'Input;
use Illuminate'Support'Facades'Response;
use Usage;
use Auth;

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) 设置新的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);
}

感谢您的完整回答!但我已经做了一个简单的API控制器没有oAuth2包。如果登录正常并且运行良好,我的控制器现在只返回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'));
            }    
}

这里是我的api路由

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

你觉得安全管理怎么样?我可以在ios上进行自己的代币系统验证吗?

编辑

我终于找到了一个解决方案,这里是我的ApiController中的函数:你只需要从iOS向你的api发送Facebook或Google connexion生成的令牌。在我的情况下,添加一个网络参数。

 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';
    }


}