dongyuan4790 2015-03-19 16:27
浏览 33
已采纳

使用Laravel 4中的动态内容进行制作和错误视图

I'm finishing my first web based app using Laravel 4 and it's almost done, the issue I have is the following:

I'm trying to make custom 404 error view with dynamic content, I have a form with a couple of selects populated from a DB, I do this thru the Base controller like this:

protected function setupLayout() {
    if (!is_null($this->layout)) {
        $this->layout = View::make($this->layout);
    }

    View::composer('templates.partials.footer', function ($view) {
        $languages = array('' => 'Choose Language Pair:') + Language::orderBy('order')->lists('name', 'id');
        $view->with('languages', $languages);
    });
}

This form needs to be present in the entire web and it works fine in all the views but the error one, when I enter an unvalid url an php error shows telling me that the variable $languages in the footer view is undefined. I tried taking out the footer from the default template and it works wonderful.

Here is how I'm handling my errors: -in the global.php

App::error(function(Exception $exception, $code) {
    //Log::error($exception);
    return BaseController::handleError($code);
});

-and the method in the BaseController:

protected function handleError($code) {
    //return View::make('404' . $code);

    switch ($code) {
        case 403:
            return Response::view('404', array(), 403);
        case 404:
            return Response::view('404', array(), 404);
        case 500:
            return Response::view('404', array(), 500);
        default:
            return Response::view('404', array(), $code);
    }
}

As you can see I've tried using Response::view but it seems that it doesn't load the View::composer that sends the data to the form, and the other way around using return View::make isn't good either because I need it to be an error for SEO reasons.

So...

Is there a way to send my dynamic data to my error view while getting the errors in the browser?

Thanks before hand!

  • 写回答

1条回答 默认 最新

  • doucheng9304 2015-03-19 17:01
    关注

    I believe your controllers would not load during a 404 error meaning your BaseController would never be hit.

    I'd suggest placing your view composers logic in your basecontroller into app/start/global.php instead.

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

报告相同问题?