douhan8581 2014-10-07 02:27
浏览 51
已采纳

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

I'm new at Laravel. and i got a little problem with view.

here is my controller

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);

}

and here is my layouts/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

my routes:

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

when i try to run the index.blade.php it says: Undefined variable: mainmenus (View: C:\xampp\htdocs\mestika\app\views\layouts av.blade.php) (View: C:\xampp\htdocs\mestika\app\views\layouts av.blade.php) (View: C:\xampp\htdocs\mestika\app\views\layouts av.blade.php)

what i want to do is i want to show main_menu at every view pages. i've tried changed view::make to view::share. but no luck. is there anything i miss at here?

  • 写回答

1条回答 默认 最新

  • doudi1750 2014-10-07 03:03
    关注

    Use a view composer for the menu/nav, so you don't have to build the navigation in every view manually, for example, put this code in a file named nav.php and put the file inside app/start folder then include it from app/start/global.php file:

    // 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);
    
    });
    

    In your app/start/global.php file (at the bottom) add this:

    require 'nav.php';
    

    Now, every time you extend the layouts_index.blade.php your menu items ($mainmenus, $submenus, $productmenus) will be available in the main layout. So, you may use something like this in the layout:

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

    Now, in your Controller, when you are passing any data to any view that extends layouts_index you don't need to pass the menu items manually, because those will be passed by Framework by view composer you have created.

    If you don't want to pass the menu items for products then just remove the ->with('products', $products) from your view composer, so only $mainmenus and $submenus will be passed automatically to the main layout and you can pass the $products manually from any controller. Check this article.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?