dourui7186 2015-05-22 16:46
浏览 26
已采纳

Laravel在对象存在时返回错误

I have an global __construct() function inside my basecontroller, what looks like this:

public function __construct() { 
    $alerts = Alert::orderBy('id', 'DESC')->whereNull('deleted_at')->get();
    return View::share('alert', $alerts);
}

This works on every page except on 2 pages...

My route looks like this:

Route::resource('/', 'WebsiteController');

Route::get('nieuws/{id}', 'NewsController@show');

Route::resource('login', 'LoginController');

Route::resource('auth', 'LoginController@auth');

Route::resource('logout', 'LoginController@logout');

Route::resource('foto', 'PictureController');

Route::resource('studio', 'PictureController@showStudioPics');

//special routing for the mirrors
Route::get('spiegels', 'PictureController@getPicList');

Route::get('spiegels/{dirName}', 'PictureController@showPicList');
//end of special routing

Route::resource('calendar', 'CalendarController');

Route::get('history', function()
{
    return View::make('home.history');
});

Route::get('publicity', function()
{
    return View::make('home.publicity');
});

The pages that aren't working are the following:

  • publicity
  • history

The error I get is Undefined variable: alert but that's weird, because inside my view I have this:

@foreach($alert as $alert)
<div class="alert alert-{{ $alert->type }}" role="alert" style="margin-bottom:-10px;">{{ $alert->message }}</div>
@endforeach

And on all the other pages it does work.

What do I wrong?

  • 写回答

1条回答 默认 最新

  • donglan6777 2015-05-22 16:58
    关注

    The two views that aren't working are not using any controller and thus are not extending the BaseController. If you create a controller for them which extends the Base one instead of having the function inside the route file then your issue is resolved.

    Do this as follows:

    1) Create a controller "ContentControler.php" which extends BaseController exactly like your other controllers do already.

    Then have two methods:

    public function getHistory()
    { 
    return view('home.history');
    }
    
    public function getPublicity()
    { 
    return view('home.publicity');
    }
    

    and in your routes:

    change this:

    Route::get('history', function()
    {
        return View::make('home.history');
    });
    
    Route::get('publicity', function()
    {
        return View::make('home.publicity');
    });
    

    to this

    Route::get('history', 'App\Http\Controllers\ContentController@getHistory')
    Route::get('publicity', 'App\Http\Controllers\ContentController@getPublicity')
    

    *** In Laravel 4 Controllers do not fall under the App\Http\Controllers namespace so in routes you only need to declare the controllerName@methodName without the namespace which is a new feature of Laravel 5.

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

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接