[英]Laravel headers not being sent
我有一个自定义控制器正在为我处理404错误。 这是因为我将页面存储在数据库中,所以当发生404时,它将首先检查数据库以查看是否有该URL的页面,如果没有,则它应返回404。我的控制器还在构造函数中获取其他信息,页面需要的,因此为什么我不只是使用abort(); 这是我的代码:
<?php namespace App\Http\Controllers;
use App\Menu_item;
use Session;
use Auth;
use View;
use App\Page;
class FrontendController extends Controller {
public function __construct()
{
$this->data = array();
$this->data['main_menu'] = $this->get_menu_items(1);
$this->data['mobile_main_menu'] = $this->get_menu_items(2);
$this->data['quick_links'] = $this->get_menu_items(3);
$this->data['information'] = $this->get_menu_items(4);
$this->data['message'] = Session::get('message');
$this->data['user'] = Auth::user();
}
public function page($url)
{
$page = Page::where('url', '=', $url)->first();
if(!is_null($page)) {
$this->data['page'] = $page;
return View::make('pages/cms_page', $this->data);
} else {
return response()->view('errors/404', $this->data)->header('404', 'HTTP/1.0 404 Not Found');
}
}
function get_menu_items($menu_id, $parent_id=0)
{
$items = Menu_item::where('menu_id', '=', $menu_id)->where('parent_id', '=', $parent_id)->orderBy('sort_order', 'asc')->get();
foreach($items as $item) {
$item->children = $this->get_menu_items($menu_id, $item->id);
}
return $items;
}
}
如果我在开发人员工具中查看响应,尽管该页面报告的状态为200正常。 如何抛出适当的404并仍然呈现视图?
更改此:
return response()->view('errors/404', $this->data)->header('404', 'HTTP/1.0 404 Not Found');
对此:
return response()->view('errors/404', $this->data, 404);
view()
的第三个参数是要包含在请求中的状态代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.