Laravel:发送变量失败(未定义变量)

时间:2014-10-07 02:27:26

标签: php laravel

我是Laravel的新人。我的观点有点问题。

这是我的控制器

public function main_menu()
{   
    $mainmenus = DB::table('main_menus')->get();

    $subs = $mainmenus;
    foreach ($subs as $key => $value) 
    {
        $subs[$key] = DB::table('sub_menus')
                        ->where('parent_id', '=' , $value->main_id)
                        ->get();
    }

    $products = DB::table('sub_menus')->get();
    foreach ($products as $key => $value) {
        $products[$key] = DB::table('products')
                                -> where('product_parent', '=', $value->sub_id)
                                ->get();
    }

    $data = array("mainmenus" => $mainmenus,
            "submenus" => $subs,
            "productmenus" => $products);

    return View::make('layouts/nav', $data);

}

这是我的布局/ nav.blade.php

 @extends('layouts_index')

 @section('nav')
 <div class="nav">
 <nav class="navbar">
    <ul id="menu">

        <li class="active"><a href="index" class="drop"><i class="icon-home icon-small">  </i>Home</a></li>

        @foreach ($mainmenus as $key=>$mainmenu) 
            <li>
                <a href="menu/{{ $mainmenu->main_id }}" class="drop">
                <i class="icon-{{ $mainmenu->main_icon }} icon-small"></i>{{ $mainmenu->main_title_id }}</a>
                @if( $mainmenu->main_desc_id != "")
                    <div class="dropdown_3columns">
                        <div class="col_4">
                            <h3><i class="icon-{{ $mainmenu->main_icon }} icon-large"></i>{{ $mainmenu->main_title_id }}</h3>
                            <h4>{{ $mainmenu->main_desc_id }}</h4>  
                        </div>
                        @foreach ($submenus[$key] as $key=>$submenu) 
                            <div class="col_1">
                                <a href="" ><h5><i class="icon-small icon-bookmark"></i>{{ $submenu->sub_title_id }}</h5></a>   

                                <ul>
                                @foreach ($productmenus[$key] as $productmenu)
                                    <li>
                                        <a href="" ><i class="icon-small icon-th-large"></i>{{ $productmenu->product_title_id }}</a>
                                    </li>   
                                @endforeach

                                </ul>
                            </div>

                        @endforeach

                    </div>
                @endif
            </li>
        @endforeach
        <li class="last"><a href="#" class="drop "><i class="icon-pencil icon-small"></i>Tentang Kami</a></li>
    </ul> 
  </nav>
 </div>

@stop

我的路线:

  Route::get('/', 'ShowsController@index');

当我尝试运行index.blade.php时,它说: 未定义的变量:mainmenus(查看:C:\ xampp \ htdocs \ mestika \ app \ views \ layouts \ nav.blade.php)(查看:C:\ xampp \ htdocs \ mestika \ app \ views \ layouts \ nav.blade。 php)(查看:C:\ xampp \ htdocs \ mestika \ app \ views \ layouts \ nav.blade.php)

我想要做的是我想在每个视图页面显示main_menu。我已经尝试将view :: make改为view :: share。但没有运气。我有什么想念的吗?

1 个答案:

答案 0 :(得分:0)

使用view composer菜单/导航,因此您不必手动在每个视图中构建导航,例如,将此代码放在名为nav.php的文件中并放入然后app/start文件夹中的文件包含app/start/global.php文件:

// app/start/nav.php
View::composer('layouts_index', function($view){

    // Prepare the menuitems here

    $mainmenus = DB::table('main_menus')->get();
    $subs = 'Prepare it here...';
    $productmenus = 'Prepare it'

    return $view->with('mainmenus', $mainmenus)
                ->with('submenus', $submenus)
                ->with('products', $products);

});

app/start/global.php文件(底部)中添加:

require 'nav.php';

现在,每次扩展layouts_index.blade.php菜单项($ mainmenus,$ submenus,$ productmenus)都将在主布局中提供。所以,你可以在布局中使用这样的东西:

@if(isset($mainmenus))
    @foreach ($mainmenus as $key => $mainmenu)
        {{ 'Use it here...' }}
    @endforeach
@endif

现在,在您的Controller中,当您将任何数据传递到任何扩展layouts_index的视图时,您不需要手动传递菜单项,因为这些将通过{您创建的Framework {1}}。

如果您不想传递view composer的菜单项,那么只需从您的视图编辑器中移除products,这样只有->with('products', $products)会自动传递给主视图。布局,您可以从任何控制器手动传递$mainmenus and $submenus。检查this article