如何设置Laravel 5.2以连接到API

时间:2016-07-04 09:39:49

标签: php laravel-5.2

我有一个后端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;
    }

}

1 个答案:

答案 0 :(得分:1)

我已经设法通过模仿DatabaseUserProvider来自行解决这个问题。

  1. 使用子文件夹App\BlahApp\Blah\Auth

  2. 创建文件夹App\Blah\TCP
  3. 创建新的用户提供商

    > php artisan make:provider App\Blah\Auth\BlahUserProvider
    
  4. \vendor\laravel\framework\src\Illuminate\Auth\DatabaseUserProvider.php的内容复制到新的提供商(BlahUserProvider.php),并将类名更改回BlahUserProvider

  5. 创建了App\Blah\TCP\TCPClient.php并将我问题中的课程内容复制到此文件中。

  6. 更改名称空间并使用TCPClient中的stdClassBlahUserProvider.php

    namespace App\Blah\Auth;
    use App\Blah\TCP\TCPClient;
    use stdClass;
    
  7. 用{/ 1>替换retrieveByCredentials中的函数BlahUserProvider的内容

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

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

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