douhezhan5348 2014-05-25 02:20
浏览 49
已采纳

json属性,转向变量然后转移到视图

I have a json file with custom data, a list of some game servers. The json file contains the name and other data...

{
 "gameServer1": {
    "name": "game server",
    "ip": "game.gameservers.com",
    "port": "25565",
    "about": "About this game server",
    "nav": {
        "navigationLink1": {
            "name": "Forum",
            "link": "gameserver.com/someurl"
        }
    }
  // etc.. There would be quite a few other servers listed...
 }   
}

Now, in my HomeController (since this is where the data would be presented) I am pretty lost, all I have is..

public function show()
{
    $this->layout->content = View::make('home')->with('servers', $this->getServers());
}

public function getServers(){
    $file = file_get_contents(app_path() . '/views/servers.json');
    $servers = json_decode($file);
    return $servers;
}

And I am pretty sure that's wrong. I just don't know how to do this correctly. What I need to do is pass the properties to my home view

public function show()
{
    $this->layout->content = View::make('home')->with($this->getJSON());
}

So I can foreach the results and have something like this presented...

    <div class="server">
        <h3 class="server-name">{{n $name }}</h3>
        <div class="ip-address">
            {{ $ipaddress }} 
        </div><!-- /.ip-address -->
        <div class="about-server">
            {{ $about }}
        </div><!-- /.about-server -->
        <div class="server-nav">
        <div class="nav-info">
            <strong>Quick Links</strong>
        </div>
            <ul>
                <li><a href="">{{links}}</a></li>

            </ul>
        </div>
 etc....
</div><!-- /.server-container -->

I feel like I'm not even close to figuring out how to do this though. What to do?

  • 写回答

1条回答 默认 最新

  • dua55014 2014-05-25 04:17
    关注

    To pass the data to your view you should give a name in the with method so using that given name you'll be able to access the data in your view, for example you have following code now:

    $this->layout->content = View::make('home')->with($this->getJSON());
    

    You need to pass a name (anything) for the variable like this:

    $this->layout->content = View::make('home')->with('servers', $this->getJSON());
    

    Now you can access the data in your view by referring the $servers variable. Since your $servers variable will contain an array of stdClass so you may loop the $servers variable in the view like this:

    <div class="server">
      @foreach($servers as $server)
        <h3 class="server-name">{{ $server->name }}</h3>
        <div class="ip-address">
            {{ $server->ip }} 
        </div>
        <div class="about-server">
            {{ $server->about }}
        </div>
        <div class="server-nav">
        <div class="nav-info">
            <strong>Quick Links</strong>
        </div>
            <ul>
                @foreach($server->nav as $linkObj)
                    <li><a href="{{ linkObj->link }}">{{ $linkObj->name }}</a></li>
                @endforeach
            </ul>
        </div>
      @endforeach
    </div>
    

    Each {{ }} will print out the $server->properties from the stdClass object. This is an example of Blade template and when you call this:

    $this->layout->content = View::make('home')->with('servers', $this->getJSON());
    

    The framework looks for the view in app/views/home.blade.php so make sure you have created the home.blade.php view file in app/views folder. This is just a simple idea but you need to read the documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab