我需要通过Controller To View传递Json。怎么样?

时间:2018-12-30 07:44:49

标签: json laravel

我已经完成了这个任务。我所要做的就是通过控制器传递Json文件(意思是从存储中读取它),然后将其返回到视图并显示。

My Controller Function

public function ReadFromStorage(){

        $path = Storage::get(storage_path('widget.json'));

        return   view('read', compact(path));
    }


My Route

Route::get('/read','ReadJsonController@ReadFromPublic');

2 个答案:

答案 0 :(得分:0)

我相信这会起作用:

My Controller Function

public function ReadFromPublic(){

        $path = Storage::get('widget.json');

        return view('read', compact('path'));
    }


My Route

Route::get('/read','ReadJsonController@ReadFromPublic');

如果您使用的是公用磁盘(默认使用),请不要忘记运行php artisan storage:link 您可以在此处了解更多信息:https://laravel.com/docs/5.7/filesystem

答案 1 :(得分:0)

<?php

                          $active ='';
                          try {#displays categories existing in database
                              $sql = 'SELECT * FROM category';
                              $stmt = $pdo->query($sql);
                              $stmt->bindColumn('name', $name);
                              while ($result = $stmt->fetch(PDO::FETCH_BOUND)) {
                                if(isset($_GET['category'])  && $_GET['category']=== $name){
                                  $active = 'active';
                                }else{
                                  $active = '';
                                }
                                  echo "<li class='{$active}'><a href='index.php?category=$name' class='ctgy_link'>$name</a></li>";
                              }
                          } catch (PDOException $e) {
                              $error = $e->getMessage();
                              $result = array();
                          }

                           ?>

//示例json文件

<?php require "_header.php"; ?>