How do you include the javascript helper in the ErrorHanlder?

百般思念 提交于 2019-12-12 01:54:18

问题


I have this custom error handler:

`class AppError extends ErrorHandler {

function error404($params) { $this->controller->layout = 'public'; $this->controller->set('title','Droptor Page Not Found'); parent::error404($params); } }`

And I can't seem to use any layout that has this: $javascript->link('jquery',true)

So the JS helper isn't loaded. But if I include this in the controller: var $helpers = array('javascript'); it still doesn't work. Nor does App::import('Helper', 'javascript');


回答1:


Crap, I didn't read your question.

To add a helper to your error controller, just add this line:

$this->controller->helpers = array('Javascript');

There are two ways to do it:

First, you can create an app_controller to include every component and helper that you need on all your controllers.

Second, you can load the specific resources needed to your error controller. Create a file named error.php in your app's root (NOT webroot) with the following code:

<?php
class AppError extends ErrorHandler  {
    function error404($params) {
        $this->controller->helpers = array('Javascript');
        parent::error404($params);
    }
}

You can also set a custom title with

$this->controller->set('title_for_layout', "We couldn't find what you are loooking for");

Good luck.




回答2:


First off, you don't need to create your own handler if all you're doing is handling a common error type, such as 404. Custom error handlers are for your application specific errors.

If you want to simply change the layout of your page when you get a 404 error, this has been answered over here.

function beforeRender() {
    if($this->name == 'CakeError') {
        $this->layout = false;
    }
}

And you can cause it using the line:

$this->cakeError('error404');


来源:https://stackoverflow.com/questions/4298445/how-do-you-include-the-javascript-helper-in-the-errorhanlder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!