簡體   English   中英

如何設置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\\AuthApp\\Blah\\TCP創建文件夾App\\Blah

  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. BlahUserProvider retrieveByCredentials函數的內容替換為

     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. 我也取代功能retrieveById具有相同內容的功能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. config\\auth.php的提供程序更改config\\auth.php用戶提供程序

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM