-2

/api/topicsLaravel では、次の例で、ルートからの json 応答を$topicsバックエンドの変数に割り当てるにはどうすればよいですか?

API:

Route::get( '/api/topics', function()
{
    return Topic::all();
});

バックエンド:

Route::get( '/backend/topics', function()
{
    $topics = // call to route /api/topics goes here;

    return View::make( 'backend.topics' )->with( array( 'topics' => $topics ) );
});
4

1 に答える 1

0

URL を呼び出すと、JSON オブジェクトは返されませんが、デコードする必要がある JSON 文字列が返されます。ページのコンテンツを取得するには、file_get_contents を使用するだけです。

Route::get( '/backend/topics', function()
{
    $topics = file_get_contents('/api/topics');
    // you may test if $topics !== FALSE
    $topics = json_decode($topics);

    return View::make( 'backend.topics' )->with( array( 'topics' => $topics ) );
});

これが簡単な方法です。PHP REST クライアントを使用して、HTTP ステータス コードなどを処理したい場合があります...

于 2013-04-07T00:13:40.750 に答える