2016-07-04 3 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

다른 찾을 수 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']); } 
  • 가 어쩌구 사용자 공급자에 config\auth.php에서 공급자를 변경 내 새 드라이버도 추가 기능을 포함하도록 \vendor\laravel\framework\src\Illuminate\Auth\CreatesUserProviders.phpcreateUserProvider 기능을 확장 유사한 상황 [여기] (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을 가진 폴더 App\Blah를 만들고 클래스를 변경 다시 BlahUserProvider으로 이름을 변경하십시오.

    3. 만든 App\Blah\TCP\TCPClient.php 내 질문에이 클래스의 내용을이 파일에 복사했습니다.

    4. 변경은 네임 스페이스와 내가 같은 내용으로 기능 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); 
      } 
      
    5. BlahUserProvider에 기능 retrieveByCredentials의 내용을 대체 BlahUserProvider.php

      namespace App\Blah\Auth; 
      use App\Blah\TCP\TCPClient; 
      use stdClass; 
      
    6. TCPClientstdClass 사용 기능으로 retrieveByCredentials 지금은 사용자가 로그인 할 수 있도록 b 나는 여전히 C++ 애플리케이션에서 요청을 생성해야한다.

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

    이것은 매우 도움이됩니다. 그것을 쓸 시간을내어 주셔서 감사합니다. – manshu

    관련 문제