Laravel 5.4如何从不同的控制器向master.blade.php发送正确的变量

时间:2017-06-18 12:55:24

标签: master-pages laravel-5.4

我是Laravel的新手。我搜索了这个网站,但无法找到具体的帮助。

我设置了这样的路线:

Route::get('/', 'MainPageController@index');
Route::get('/', 'MainPageController@menu');

//www.blabla.com/news
Route::get('news', 'NewsController@index');

//www.blabla.com/new/here-is-slag
Route::get('new/{new}', 'NewsController@show');


//www.blabla.com/events
Route::get('events', 'EventsController@index');

//www.blabla.com/event/here-is-slag
Route::get('event/{event}', 'EventsController@show');


//www.blabla.com/projects
Route::get('projects', 'ProjectsController@index');

//www.blabla.com/project/here-is-slag
Route::get('project/{project}', 'ProjectsController@show');

这是我的master.blade.php页面:

<html>
    <head>
        <title>Blabla @yield('title')</title>

    </head>
    <body>
        <nav class="navbar navbar-light bg-faded rounded navbar-toggleable-md">
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#containerNavbar" aria-controls="containerNavbar" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="containerNavbar">
        <ul class="nav navbar-nav ml-auto">
            <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown04" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Categories</a>
            <div class="dropdown-menu" aria-labelledby="dropdown04">
              <a class="dropdown-item" href="#">...</a>
            </div>
          </li>
            @foreach($menus as $menu)
              <li class="nav-item">
                <a class="nav-link" href="{{$menu->slag}}">{{$menu->link}}</a>
              </li>
            @endforeach

        </ul>
      </div>
    </nav>

          @yield('content')


    </body>
</html

当我将数据从MainPageController发送到master.blade.php时,我得到空白内容,但变量菜单包含要显示的数据,但如果我评论MainPageController @菜单的路径,我会得到包含内容的主页,但是输出菜单数据。

这是MainPageController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Banner;
use App\News;
use App\Event;
use App\Project;
use App\Menu;

class MainPageController extends Controller
{
    public function index() {

        $banners = Banner::All();

        $cardNew = News::where('active', 1)->orderBy('created_at', 'desc')->first();
        $cardEvent = Event::where('active', 1)->orderBy('created_at', 'desc')->first();
        $cardProject = Project::where('active', 1)->orderBy('created_at', 'desc')->first();

        return view('index', compact('banners', 'cardNew', 'cardEvent', 'cardProject'));


    }

    public function menu() {

        $menus = Menu::All();
        return view('master', compact('menus'));
    }

如何以正确的顺序将几个变量发送到master.blade.php以生成链接,卡片,滑块?所有数据均来自Mysql DB。

0 个答案:

没有答案