如何设置Laravel 5.2连接到API


How to set up Laravel 5.2 to connect to API

我有一个后端c++应用程序,它使用标准TCP连接接收JSON请求。此应用程序管理所有业务逻辑(用户身份验证、事务处理、数据请求和验证)。

我需要如何设置Laravel 5.2连接到这个服务器的用户身份验证和事务处理?我不需要在Laravel方面的任何数据库,因为所有的数据将通过c++应用程序访问。

作为奖励,如果可能的话,我还希望将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;
    }
}

我已经设法通过模仿DatabaseUserProvider对自己进行排序。

  1. 创建文件夹App'Blah,并创建子文件夹App'Blah'AuthApp'Blah'TCP

  2. 创建新的用户提供者

    > php artisan make:provider App'Blah'Auth'BlahUserProvider
    
  3. 'vendor'laravel'framework'src'Illuminate'Auth'DatabaseUserProvider.php的内容复制到新的提供程序(BlahUserProvider.php)中,并将类名更改为BlahUserProvider

  4. 创建App'Blah'TCP'TCPClient.php并将问题中类的内容复制到此文件

  5. 更改命名空间,在BlahUserProvider.php中使用TCPClientstdClass

    namespace App'Blah'Auth;
    use App'Blah'TCP'TCPClient;
    use stdClass;
    
  6. BlahUserProviderretrieveByCredentials函数的内容替换为

    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);
    }
    
  7. 我还用与函数retrieveByCredentials相同的内容替换了函数retrieveById,现在只是为了用户可以登录,因为我仍然需要在c++应用程序中创建请求。

  8. 扩展'vendor'laravel'framework'src'Illuminate'Auth'CreatesUserProviders.php中的createUserProvider功能以包括我的新驱动程序,并添加了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. config'auth.php中的提供者更改为blah用户提供者

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