2017-10-23 2 views
0

を構築するためにパラメータを渡す:PHPは関数を呼び出すと、私が持っている社会階級持っ

protected $id; 

public function __construct($request, $id) 
{ 
    Log::info('Processing...', ['request' => $request, 'id' => $id]); 
    try { 
     $client = new Client(); 
     $url = sprintf($request); 
     $response = $client->get($url); 
     $json = json_decode((string) $response->getBody(), true); 
     return $json; 
    } catch (ClientException $exception) { 
     $responseBody = $exception->getResponse()->getBody(true); 
     Log::error($responseBody, ['entity_id' => $id]); 
    } 
} 
public function wikipedia($wikipedia_url, $id) 
{ 
    dd($json); 
    try { 
     $wikipedia_array = $json['parse']['text']; 
     $wikipedia_array = array_slice($wikipedia_array, 0, 9); 
     $wikipedia_array = implode($wikipedia_array, ','); 
     Log::info('Processed Wikipedia for', ['entity_id' => $id]); 
     return $wikipedia_array; 
    } catch (Exception $e) { 
     Log::error('Wikipedia:', ['message' => $e->getMessage(), 'entity_id' => $id]); 
    } 
} 

を私はこのようなファサード呼び出しています別の関数では:私はこれを取得しかし

$id = $entity->id; 
    $wikipedia_id = $entity->wikipedia; 
    if (!empty($wikipedia_id)) { 
     $wikipedia_url = 'http://en.wikipedia.org/w/api.php?action=parse&prop=text&section=0&disablelimitreport=1&format=json&page='.$wikipedia_id; 
     $wikipedia_html = Social::wikipedia($wikipedia_url, $id); 
     Log::info('Wikipedia ok for', ['entity_id' => $id]); 
    } 

を:

Type error: Too few arguments to function App\Helpers\Social::__construct(), 0 passed in /home/vagrant/liveandnow/app/Providers/SocialServiceProvider.php on line 35 and exactly 2 expected

どのようにしてメソッドを呼び出すか、パラメータを渡すだけでなく、それらを渡して構築する方法を説明できますか?

<?php 

namespace App\Facade; 

use Illuminate\Support\Facades\Facade; 

class Social extends Facade 
{ 
    /** 
    * Get the registered name of the component. 
    * 
    * @return string 
    */ 
    protected static function getFacadeAccessor() 
    { 
     return 'social'; 
    } 
} 

とサービスプロバイダ:

public function register() 
{ 
    $this->app->bind('social', function ($app) { 
     return new Social; 
    }); 
} 

答えて

0

エラーは、サービスプロバイダである

は、ここに私のファサードです。 あなたは2つのパラメータ、

public function __construct($request, $id)

でコンストラクタを定義しますが、あなたのサービスプロバイダーにあなたはこのようにそれを呼び出す:

public function register() 
{ 
    $this->app->bind('social', function ($app) { 
     return new Social; 
    }); 
} 

あなたは社会的なクラスをインスタンス化する際に、たとえば、両方の引数を追加する必要があります

return new Social("http://xyz.de", 1);

などの希望、このことができます。

+0

あなたの例ではあなたがそれらをハードコーディングしているが、私は$ url、$ idとしてそれらを渡す必要があるので、私はそれらを '動的に'どのように渡すのですか? –

関連する問題