2017-07-08 16 views
0

要呼叫控制器I使用這樣的CURL請求:呼叫Laravel控制器,並通過HTTP標頭它

curl -kX POST https://site/method -H 'Authorization: Bearer ACCESS_TOKEN\ 
Content-Type:application/json' -d ' 
{ 
    "param1":"value 1", 
    "param2":"value 2", 
    "param3":"value 3" 
}' 

的呼叫路由UserController的@方法。

如何從Laravel代碼(內部請求,不發送CURL)執行相同操作?

我發現用這樣的

 $controller = app()->make($controllerName); 
     $response = app()->call([$controller, 'method'], []); 
     echo $response; 

其中最後[]應該包含一些參數的建議。但如果是這樣的話,我無法弄清楚陣列應該如何看待我的情況。

P.S.請不要回答「這是一個壞習慣」,而不是給出建議如何實施我所需要的。

回答

0

我沒有看到一個簡單的解決方案來做到這一點。從其他方法調用控制器不是一個好主意。控制器響應請求,不應該被用作微不足道的類。如果您需要測試控制器,laravel提供測試方法,包括調用控制器。 https://laravel.com/docs/5.4/testing。如果您需要在幾個地方調用某種方法 - 最好在模型或存儲庫中定義它,然後在需要的地方調用它。

0

這是我的解決方案(Laravel 5.4)。通過https://stackoverflow.com/a/40366119/518704的啓發,但需要多少時間深入到代碼中查找如何通過我的授權:承載令牌

我需要這個方法在瀏覽器中無套接字連接(救護車),其中JS代碼授權沒有運行。所以我不能使用棘輪WebSocket,只有簡單的插座Ratchet IoServer。我的API控制器返回JSON,所以很容易進一步處理響應。我需要這種方法來主要驗證套接字連接,假設客戶端在打開套接字時知道訪問令牌(在登錄時由REST獲取)。所有的

首先我需要使用一個特點,在測試

下一頁在的onMessage代碼中使用我認爲一個JSON消息與訪問令牌傳遞。爲了簡化這裏我就不寫了JSON有效性檢查等

namespace App; 

use Ratchet\ConnectionInterface; 
use Askedio\LaravelRatchet\RatchetServer as RatchetServerBase; 

class RatchetServer extends RatchetServerBase 
{ 
... 
    public function onMessage(ConnectionInterface $conn, $input) 
    { 
      $input = json_decode($input); 
      $token = $input->token; 


      // Some data (may be taken from JSON $input) 
      $data = [ 
      "param1" => "value 1", 
      "param2" => "value 2", 
      "param3" => "value 3" 
      ]; 

      // Prepare the server headers 
      $server = ['HTTP_AUTHORIZATION' => 'Bearer ' . $token]; 

      // This way! If calling app() here, it reuses the same Auth::user() 
      $app = require __DIR__.'/../bootstrap/app.php'; 
      $kernel = $app->make(\Illuminate\Contracts\Http\Kernel::class); 
      $response = $kernel->handle(
       $request = \Illuminate\Http\Request::create('/user', 'GET', $data, [], [], $server) 
     ); 
      // ~ var_dump(Auth::id()); 
      $controllerResult = $response->getContent(); 
      $kernel->terminate($request, $response); 

      // My controller hidden under /user route is an API one and returns JSON string 
      echo $controllerResult; 
    } 
... 
} 

我以前的解決方案沒有奏效。 Auth :: user()總是返回 第一個登錄用戶(app()似乎使用單例)。所以我留下 以前的代碼作爲參考。

namespace App; 

use Ratchet\ConnectionInterface; 
use Askedio\LaravelRatchet\RatchetServer as RatchetServerBase; 

class RatchetServer extends RatchetServerBase 
{ 
    use \Illuminate\Foundation\Testing\Concerns\MakesHttpRequests; 

... 
    public function onMessage(ConnectionInterface $conn, $input) 
    { 
      $input = json_decode($input); 
      $token = $input->token; 

      $data = [ 
      "param1" => "value 1", 
      "param2" => "value 2", 
      "param3" => "value 3" 
      ]; 

      // The 2 lines below can me moved to the __constructor method 
      $this->baseUrl = request()->getSchemeAndHttpHost(); 
      $this->app  = app(); 

      // Prepare the server headers 
      $server = ['HTTP_AUTHORIZATION' => 'Bearer ' . $token]; 

      // Call my controller by route (this seems to be internal call) 
      $response = $this->call('GET','/method', $data, [], [], $server)->json(); 
    } 
... 
} 

附:當我稍後再來時,請留意我自己。太看到可用的標題和正確的命名我做了一個var_dump爲$this->parameters./vendor/symfony/http-foundation//ServerBag.php