Thinkphp5笔记七:设置错误页面②

更加完美的去设置错误页面。

一、准备一个错误页面 error.html,位置:thinkphp\template\index\default\error.html ,准备把前段所有的错误提示都指向这里。

二、空操作指向

在apps\index\controller\IndexBase.php,“基类”里面设置_empty

<?php
/**
 * 前端基类
 * */
namespace app\index\controller;
use  app\Common\controller\Base;

class IndexBase extends  Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 空操作 跳转
     * */
    public function _empty(){
        //abort();
        exception();     //  这两种方法都可以
    }

}

三、空控制器指向

在apps\index\controller\Error.php

<?php
/**
 * 空控制器跳转
 * */
namespace app\index\controller;
use app\index\controller;

class Error extends IndexBase
{
    public function index(){
        abort();
    }

}

四、异常错误指向 

在index/config.php  exception_tmpl 参数。

‘exception_tmpl‘         => THINK_PATH . ‘tpl‘ . DS . ‘think_exception.tpl‘,
 //‘exception_tmpl‘ =>‘E:/wamp/www/thinkphp/template/index/default/error.html‘,

注意:地址一定要绝对路径。

拓展,

401,404,500等错误页面自定义

相关参数:http_exception_template

手册地址:http://www.kancloud.cn/manual/thinkphp5/163256

代码:

config.php

‘http_exception_template‘    =>  [
        // 定义404错误的重定向页面地址
        404 =>  ROOT_PATH.config(‘template.view_path‘).config(‘index.model_name‘).‘/‘.config(‘index.default_template‘).‘/404.html‘,
        // 还可以定义其它的HTTP status
        401 =>  ROOT_PATH.config(‘template.view_path‘).config(‘index.model_name‘).‘/‘.config(‘index.default_template‘).‘/401.html‘,
    ],

控制器调用

abort(404,‘错误信息‘)

error.html,404.html  页面代码,可以参考thinkphp\thinkphp\tpl\think_exception.tpl

时间: 2024-08-17 16:18:07

Thinkphp5笔记七:设置错误页面②的相关文章

rails resce_from设置错误页面

1.设置ActiveRecord find方法没找到不报错,调用一个显示404页面的方法 class ApplicationController < ActionController::Base   rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found   def record_not_found     render 'record_not_found'     # ....     true   end e

Thinkphp5笔记七:设置错误页面①

设置网站的错误提示页面,也是一个很重要的环节. 一.空操作 在当前控制器里面增加E_empty操作 public function _empty(){ $this->error('方法不存在'); } Public function index(){ } 测试方法: 正常: http://localhost/thinkphp/index/index/index 错误: 会提示"方法不存在" http://localhost/thinkphp/index/index/df 二.空控

thinkphp5设置404页面不跳转

thinkphp5设置404页面的步骤: 1. 首先关闭调试模式,即配置application/config文件,使'app_debug' => false 2. 添加自定义404页面跳转地址,即配置application/config文件 'http_exception_template' => [ 404 => '404/404.html', #跳转地址 ], 正常情况下,上面就已经完成了设置.而我却遇到的问题. #1 访问默认页面(已设置过路由):index/index可以正常访问

nginx + tomcat 架构中,error_page错误页面的设置

如果是单独的nginx设置404或者403等错误页面时,只需要如下即可: fastcgi_intercept_errors on:       error_page 404  /404.html: 但是,如果nginx为后台tomcat做代理时,上述方法就搞不定了,那要如何,如下: proxy_intercept_errors on:   #有意思的是,这个开关默认是关闭的,所以得配置打开 error_page 404 /404.html: nginx为tomcat做代理时,404或者403等错

LNMP设置404错误页面

LNMP即Linux+Nginx+MySQL+PHP的web架构.     1.在网站的根目录touch 文件404.html,内容如下: <html> <body><H1>404 NOT FOUND!</H1></body> </html> 2.chmod 755  404.html chown www:www   404.html 3.修改nginx配置文件nginx.conf: 在http {}区域添加上:fastcgi_int

ASP.NET网站中设置404自定义错误页面

在用ASP.NET WebForm开发一个网站时,需要自定义404错误页面. 做法是这样的 在网站根目录下建立了一个404.html的错误页面,然后在Global.asax文件中,加入如下代码: <%@ Application Language="C#" %> <script runat="server"> void Application_Error(object sender, EventArgs e) { Response.Status

nginx 设置错误的自定义404页面跳转到500

5年前写的站, 当时是在apache下写的error page, 换了nginx后, 404页面直接跳到了500 服务器内部错误. 只需要在配置 try_files 的时候指定一下就可以了 我的是debian, 在/etc/nginx/site-available/default 里面location / server{ </pre><pre name="code" class="plain"> location / { <span s

织梦网站如何设置404错误页面?

首先在网上搜索织梦系统404错误页面的一个网页或者也可以自己编辑,然后在通过网页编辑器编辑一下这个404网页,然后把里面的网址改为自己的网址,佐言用的是 Macromedia Dreamweaver 8工具编辑的,如下图: 第一步.修改404.html网页里面的信息 然后在看一下自己网站的编码格式是什么,然后在通过Macromedia Dreamweaver 8工具修改下载的404.html网页的编码格式.找到Macromedia Dreamweaver 8工具上面"修改":修改-页面

Apache服务器上设置404错误页面

Apache服务器上设置404错误页面 基于ubuntu 16.04系统,使用 apache 进行设置 配置步骤 添加404页面 在所配置网站的根目录添加编辑好的 404.html 修改localized-error-pages.conf配置文件 localized-error-pages.conf位于 /etc/apache2/conf-enabled目录下 把#ErrorDocument 404 /404.html 去掉# 重启 apache 服务器 /etc/init.d/apache2