2016-07-04 11 views
0

標準TCP接続を使用してJSONリクエストを受け取るバックエンドC++アプリケーションがあります。このアプリケーションは、すべてのビジネスロジック(ユーザー認証、トランザクション処理、データ要求および検証)を管理します。APIに接続するためのLaravel 5.2の設定方法

ユーザ認証とトランザクション処理のためにこのサーバに接続するには、Laravel 5.2をどのように設定する必要がありますか?すべてのデータがC++アプリケーションを介してアクセスされるため、Laravel側にデータベースは必要ありません。

これも可能な場合は、JWTをユーザー認証用に組み込むこともできます。

以下のコードは、現在標準のPHPを使用してアプリケーションサーバーに接続している方法です。私は同じ機能を望んでいますが、より多くのLaravelの方法で。

class tcp_client 
{ 
    private $sock; 

    function __construct() 
    { 
     // create the socket 
     $this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
     if (!is_resource($this->sock)) 
     { 
      // throw exception 
     } 

     // set socket options 
     $this->set_options(); 
    } 

    function connect($host, $port) 
    { 
     $timeout = 3; 
     $startTime = time(); 
     while (!socket_connect($this->sock, $host, $port)) 
     { 
      if ((time() - $startTime) >= $timeout) 
      { 
       // throw exception 
      } 
      sleep(1); 
     } 
    } 

    private function set_options() 
    { 
     if (!socket_set_option($this->sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 5, 
        'usec' => 0))) 
     { 
      // throw exception 
     } 

     if (!socket_set_option($this->sock, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 5, 
        'usec' => 0))) 
     { 
      // throw exception 
     } 
    } 

    public function request($request) 
    { 
     // the first 6 characters will indicate the length of the JSON string 
     $request = str_pad(strlen($request), 6, '0', STR_PAD_LEFT) . $request; 

     //Send the message to the server 
     if (!socket_send($this->sock, $request, strlen($request), 0)) 
     { 
      // throw exception 
     } 

     //Now receive header from server 
     $header = 0; 
     if (socket_recv($this->sock, $header, 6, MSG_WAITALL) === FALSE) 
     { 
      // throw exception 
     } 

     //Now receive body from server 
     $body = ""; 
     if (socket_recv($this->sock, $body, $header, MSG_WAITALL) === FALSE) 
     { 
      // throw exception 
     } 

     return $body; 
    } 

} 
+0

config\auth.phpでプロバイダを変更同様の状況[ここ]で苦労している人々(https://www.reddit.com/r/laravel/comments/ 3b4lu2/correct_structure_for_consuming_rest_api_with /) – WitHeld

答えて

1

私はDatabaseUserProviderを模倣することにより、自分でこれを整理するために管理しています。

  1. 新しいプロバイダ(BlahUserProvider.php)に\vendor\laravel\framework\src\Illuminate\Auth\DatabaseUserProvider.phpの内容をコピーし、新しいユーザープロバイダ

    > php artisan make:provider App\Blah\Auth\BlahUserProvider 
    
  2. を作成App\Blah\AuthApp\Blah\TCP

  3. サブフォルダとフォルダApp\Blahを作成し、クラスを変更名前をBlahUserProviderに戻します。

  4. App\Blah\TCP\TCPClient.phpを作成し、私の質問のクラスの内容をこのファイルにコピーしました。

  5. 変更は、名前空間とは私も同じ内容で機能retrieveByIdを置き換え

    public function retrieveByCredentials(array $credentials) 
    { 
        $tcp_request = "{\"request\":\"login\"," 
           . "\"email\":\"" . $credentials['email'] . "\"," 
           . "\"password\":\"" . $credentials['password'] . "\"}"; 
    
        $tcp_result = json_decode(str_replace("\n","\\n",$this->conn->request($tcp_request)), true); 
    
        $user = new stdClass(); 
        $user->id = $tcp_result['user']['id']; 
        $user->name = $tcp_result['user']['name']; 
    
        return $this->getGenericUser($user); 
    } 
    
  6. BlahUserProviderに機能retrieveByCredentialsの内容を置き換えBlahUserProvider.php

    namespace App\Blah\Auth; 
    use App\Blah\TCP\TCPClient; 
    use stdClass; 
    
  7. TCPClientstdClass使用します機能としてretrieveByCredentialsは今のところユーザーがログインできるようになっています私は依然としてC++アプリケーションでリクエストを作成する必要があります。

  8. が私の新しいドライバが含まれるように\vendor\laravel\framework\src\Illuminate\Auth\CreatesUserProviders.phpcreateUserProvider機能を拡張しても追加された機能createBlahProvider

    public function createUserProvider($provider) 
    { 
        $config = $this->app['config']['auth.providers.' . $provider]; 
    
        if (isset($this->customProviderCreators[$config['driver']])) 
        { 
        return call_user_func(
          $this->customProviderCreators[$config['driver']], $this->app, $config 
        ); 
        } 
    
        switch ($config['driver']) 
        { 
        case 'database': 
         return $this->createDatabaseProvider($config); 
        case 'eloquent': 
         return $this->createEloquentProvider($config); 
        case 'blah': 
         return $this->createBlahProvider($config); 
        default: 
         throw new InvalidArgumentException("Authentication user provider [{$config['driver']}] is not defined."); 
        } 
    } 
    
    protected function createBlahProvider($config) 
    { 
        $connection = new \App\Blah\TCP\TCPClient(); 
        return new \App\Blah\Auth\BlahUserProvider($connection, $this->app['hash'], $config['model']); 
    } 
    
  9. は何とかユーザープロバイダ、他の発見

    'providers' => [ 
        'users' => [ 
        'driver' => 'blah', 
        'model' => App\User::class, 
    ], 
    
+0

これは非常に親切です。それを書く時間をとってくれてありがとう。 – manshu

関連する問題