1

こんにちは、stackoverflowで問題の答えを見つけようとしましたが、何も見つからなかったようです。これが私の問題です。現在MVCフレームワークを使用しており、コントローラーのモデルから変数にアクセスする必要があります。これが私のモデルです:

    <?php

    use Guzzle\Http\Client;

    class Position_model extends CI_Model{

      public function get_location($location){

    // Create a client and provide a base URL
    $client = new Client('http://maps.googleapis.com/maps/api/geocode/json');

    $request = $client->get('?address=' . $location . '&sensor=false');

    // Send the request and get the response
    $response = $request->send();
    //decode json file to get the longitude and latitude

    $json = json_decode($response->getBody(), true);
    var_dump($json);

    if($json["results"][0]["formatted_address"] AND $json["results"][0]["geometry"]["viewport"]["northeast"]){
            $position["address"] = $json["results"][0]["formatted_address"];
            $position["latitude"] = $json["results"][0]["geometry"]["viewport"]["northeast"]["lat"];
            $position["longitude"] = $json["results"][0]["geometry"]["viewport"]["northeast"]["lng"];

            return $position;

            $code = 'success';
            $content = 'LOCATION FOUND ... I AM AWESOME';
            $this->output->set_status_header(201);
        }else{
            $code = 'error';
            $content = 'OOPPS LOCATION NOT FOUND';
            $this->output->set_status_header(400);

        }

    }

}

このクラスから$positionを取得して、scheduleというコントローラーで使用し、試した$dataという別の変数に追加する必要があります。

    $position = $this->Position_model->get_location($location)->position;
    $data += $position;

私を助けてください !!!!ただし、明らかに、これは機能せず、次のようなエラーが発生します:未定義の位置または非オブジェクトプロパティの呼び出し

4

2 に答える 2

5

問題を解決するための簡単な答え:

$position = $this->Position_model->get_location($location);
$data += $position;

しかし、コードには他の問題があります。あなたは次のようなコードを持っています

$code = 'success';
$content = 'LOCATION FOUND ... I AM AWESOME';
$this->output->set_status_header(201);

returnステートメントの後なので、実行されることはありません。したがって、プログラムの実行は決してそれに到達しません。returnステートメントの前にそれらを配置する必要があります。

また、モデルのプロパティ$this->outputを更新しないことをお勧めします。コントローラに何かを返し、戻り値に基づいて適切なHTTPヘッダーを設定します。物を返すと同時にオブジェクトの状態を変更すると、予期しない動作が発生する可能性があります。

于 2013-03-26T22:27:25.327 に答える
0

の戻り値get_locationは位置です。余分なものは必要ありません->position

あなたのコードは

$position = $this->Position_model->get_location($location);
$data += $position;

エラーは、オブジェクトのようなオブジェクトではないものを処理しようとしていることを示しています。

于 2013-03-26T22:27:31.803 に答える