CodeIgniter 默认控制器
我尝试为您访问的 URL 创建默认控制器。
意思是,自定义 404。
class MY_Exceptions extends CI_Exceptions
{
var $ci;
public function __construct()
{
parent::CI_Exceptions();
$this->ci =& get_instance();
}
function show_404($page = '')
{
//load view here
}
}
我得到 get_instance 未定义。 (因为它是在调用库之后定义的)
有没有办法在 codeignigter 1.7.2 中创建自定义 404 控制器,而无需使用系统核心代码。
谢谢
I am tring to create default controller for no matter what URL you access to.
Meaning, custom 404.
class MY_Exceptions extends CI_Exceptions
{
var $ci;
public function __construct()
{
parent::CI_Exceptions();
$this->ci =& get_instance();
}
function show_404($page = '')
{
//load view here
}
}
I am getting get_instance is undefined. (because it was defined after the libraries get called)
Is there a way to create custom 404 controller in codeignigter 1.7.2 without playing with the system core code.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
升级到 CodeIgniter 2.0 因为 其稳定并使用:
Upgrade to CodeIgniter 2.0 because its stable and use:
如果您只想自定义404页面,可以在
/system/application/errors/error_404.php
页面下进行。需要注意的是,您不能使用$this
对象,它是严格的 HTML/CSS 或内置 PHP(无法访问您的库、模型等)。这是 CodeIgniter 在找不到控制器来处理请求时默认显示的页面。
不确定这是否是您尝试这样做的原因,但这将是创建自定义 404 页面的最简单方法。
If you just want to customize the 404 page, you can do so under the
/system/application/errors/error_404.php
page. The one caveat with this is that you can't use the$this
object, it's strictly HTML/CSS or built-in PHP (no access to your libraries, models, etc.).This is the page that CodeIgniter will display by default when it can't find a controller to handle a request.
Not sure if that's why you're trying to do or not, but that would be the simplest way to create a custom 404 page.
http://maestric.com/doc/php/codeigniter_404
我已经使用了这个解决方案并且它有效对于我来说 CI 1.7
http://maestric.com/doc/php/codeigniter_404
i have used this solution and it works for me in CI 1.7
这应该可以正常工作。确保它位于“application/libraries”文件夹中,并确保您的配置将“MY_”设置为自定义库名称。
另外,如果您正在执行
function __construct(){}
,则可以执行parent::__construct()
。This should work fine. Make sure it's in the 'application/libraries' folder, and make sure your config has 'MY_' set as the custom library name.
Also, if you are doing
function __construct(){}
, you can doparent::__construct()
.