我有一条完美运作的路线。
Route::get('/advertise', 'AdvertisementController@index')->middleware('auth');
当我添加中间件并尝试访问广告页面时,我收到以下错误。
未定义的变量:user(查看:C:\ Users \ andre \ Dropbox \ College \ Project \ Rentable \ resources \ views \ layouts \ main.blade.php
这是主要布局
<header>
<div class="container"><a class="logo" href="/">Rentable</a>
<nav>
@guest
<a href="/register">Register</a>
<a href="/login">Login</a>
<a href="/advertise">Advertise Your Property</a>
@else
<a href="/advertise">Advertise Your Property</a>
<a href="/account">{{$user->name}}</a>
<a href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
@endguest
</nav>
</div>
</header>
这是广告控制器的索引
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\PropertyAdvert;
use DB;
use Auth;
class AdvertisementController extends Controller
{
public function index(){
return view('pages/Advert/create');
}
}
答案 0 :(得分:1)
更改您的控制器代码以匹配以下内容:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\PropertyAdvert;
use DB;
use Auth;
class AdvertisementController extends Controller
{
public function index(){
return view('pages/Advert/create', [
'user' => Auth::user(),
]);
}
}
您必须将user
传递给视图才能在{{ $user->name}}
或者,如果您的$user
被传递到多个页面,请在此处查看作曲家服务提供商:https://laravel.com/docs/5.6/providers