dpftppc9674 2017-04-20 09:12
浏览 318
已采纳

如何在laravel中从内部api请求获取json响应?

My route file (routes/api.php) :

Route::group(['middleware'=>'cors', 'prefix'=>'v1/'], function(){
    Route::resource('users', 'UserController');
    Route::resource('products', 'ProductController');
    Route::resource('categories', 'CategoryController');

    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
    Route::get('authenticate/user', 'AuthenticateController@getAuthenticatedUser');
});

My ProductController file():

public function index(){
    $request = Request::create('api/v1/products/', 'GET');
    $instance = json_decode(Route::dispatch($request)->getContent());
    echo "<pre>"; print_r($instance); exit;

    return view('pages.users.my_sell', ['products'=>$products]); 
}

while i try to print the $instance variable i get the following error : enter image description here

i need to get all the object from the api and need to pass the objects to my blade.

  • 写回答

2条回答 默认 最新

  • dsgwdigu84950 2017-04-20 09:54
    关注

    In your controller, have you included this?

    use \Illuminate\Support\Facades\Route;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏