【问题标题】:Passing an array to more than one view from one controller ? Laravel 5从一个控制器将数组传递给多个视图?拉拉维尔 5
【发布时间】:2017-01-10 17:36:35
【问题描述】:

我有一个关于如何从 Laravel 5 中的一个控制器将数组传递给多个视图的问题,这甚至可能吗?我想将一个数组从一个函数 index() 传递给 index.blade.php 和 home.blade.php。索引页面将显示所有数据,而主页将仅显示一些数据。下面是代码

控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\EdisiRequest;
use App\Edisi;
use Session;
use Storage;

class EdisiController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $edisi_list = Edisi::all();
        return view('edisi/index', compact('edisi_list'));
    }
}

home.blade.php

@extends('template')

@section('main')
<div class="container sitecontainer bgw">
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12 m22 single-post">
            @if (count($edisi_list) > 0)
            <div class="reviewlist review-posts">
                <?php foreach ($edisi_list as $edisi): ?>
                    <div class="post-review col-md-4 col-sm-12">
                        <div class="post-media entry">
                            <a href="#" title="">
                                <img src="{{ asset('fotoupload/' . $edisi->cover) }}" alt="fotoupload/coveredisi.png" class="img-responsive" style="height: 480px; width: 1200px;">
                            </a>
                        </div><!-- end media -->
                        <div class="post-title">
                            <h4 style="text-align: center;"><a href="#">{{ $edisi->judul }}</a></h4>
                        </div><!-- end post-title -->
                    </div><!-- end post-review -->
                <?php endforeach ?>
            </div><!-- end review-posts -->
            @else
            <p>Tidak ada data edisi.</p>
            @endif
        </div><!-- end col -->
    </div><!-- end row -->

    <div class="row">
        <div class="pagination-wrapper text-center">
            <nav>
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
</div><!-- end container -->
@stop

index.blade.php

@extends('template')

@section('main')
<div class="container sitecontainer bgw">
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12 m22 single-post">
                <div id="siswa">
                <h2>Daftar Edisi</h2>

                @if (count($edisi_list) > 0)
                <table class="table">
                    <thead>
                        <tr>
                            <th>Judul</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($edisi_list as $edisi): ?>
                            <tr>
                                <td>{{ $edisi->judul }}</td>
                                <td>
                                    <div class="box-button">
                                        {{ link_to('edisi/' . $edisi->id, 'Detail', ['class' => 'btn btn-success btn-sm']) }}
                                    </div>
                                    <div class="box-button">
                                        {{ link_to('edisi/' . $edisi->id . '/edit', 'Edit', ['class' => 'btn btn-warning btn-sm']) }}
                                    </div>
                                    <div class="box-button">
                                        {!! Form::open(['method' => 'DELETE', 'action' => ['EdisiController@destroy', $edisi->id]]) !!}
                                        {!! Form::submit('Delete', ['class' => 'btn btn-danger btn-sm']) !!}
                                        {!! Form::close() !!}
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach ?>
                    </tbody>
                </table>
                @else
                <p>Tidak ada data edisi.</p>
                @endif

                <div class="tombol-nav">
                    <a href="edisi/create" class="btn btn-primary">Tambah Edisi</a>
                </div>
            </div> <!-- / #jurnal -->
        </div><!-- end col -->
    </div><!-- end row -->
</div><!-- end container -->
@stop

我尝试为主页制作另一个控制器,但仍然无法正常工作,它总是显示错误“$edisi_list undefined variable”。如果有其他方法,请告诉我,谢谢!

EdisiServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Edisi;

class EdisiServiceProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot() {
        View::composer(['edisi/index', 'pages/home'], function ($view) {
            $edisi_list = Edisi::all();
            $view->with('edisi_list', $edisi_list);
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register() {
        //
    }

}

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    对于使用视图编辑器传递数组的任何其他人,使用 compact 传递它是行不通的。 您必须直接发送:

    在自定义作曲家中(在我的情况下为 app/Http/View/Composers/UserInfoComposer.php

    public function compose( View $view ) {
    
        $allUserInfo = array();
    
        $generalFunctions      = new GeneralFunctions();
        $currentUserID         = $generalFunctions->getCurrentUserID();
        $currentUserName       = $generalFunctions->getCurrentUserName();
        $currentUserEmail      = $generalFunctions->getCurrentUserEmail();
        $currentUserProfilePic = $generalFunctions->getCurrentProfilePic();
    
        $allUserInfo['currentUserID']         = $currentUserID;
        $allUserInfo['currentUserName']       = $currentUserName;
        $allUserInfo['currentUserEmail']      = $currentUserEmail;
        $allUserInfo['currentUserProfilePic'] = $currentUserProfilePic;
    
        $view->with( 'allUserInfo', $allUserInfo );
    //NOT: $view->with( 'allUserInfo', compact('allUserInfo'));
    
    }
    

    创建视图合成器参考:LINK

    【讨论】:

      【解决方案2】:

      您可以使用view composer 来做到这一点:

       View::composer(['index', 'home'], function ($view) {
          $edisi_list = Edisi::all();
          $view->with('edisi_list', $edisi_list);
       });
      

      【讨论】:

      • 通过使用这个所以我需要创建一个新的服务提供商?请原谅我,但是把这段代码放在哪里? @alexey
      • 是的,你应该创建一个服务提供者,你可以通过上面的链接找到示例代码。如果不想使用闭包,还需要创建视图作曲家类。
      • mm 嘿,我可以再问你一次吗?我确实使用您上面提供的代码创建了服务提供商,但它向我显示了一个新错误“EdisiServiceProvider.php 第 16 行中的 FatalErrorException:找不到类 'App\Providers\View'”。我确实将服务提供商注册到 config/app.php @alexey
      • 我已经更新了显示服务提供商代码的问题
      • 添加了“使用视图”,它现在可以工作了!非常感谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      相关资源
      最近更新 更多