0

我开发了一个 Joomla!系统插件。我想在执行该插件时检测错误的 URL。

例如

如果我输入一个 URL“ http://localhost/wrong-url ”,我想在系统插件中捕获该错误。

我怎么知道系统会显示错误页面(404)?

4

3 回答 3

0

在要捕获的系统插件中,404您必须从插件中添加一个函数作为对 JError 错误处理程序数组的回调。

我会看看com_redirect它的系统插件是怎么做的。例如

function __construct(&$subject, $config)
{
    parent::__construct($subject, $config);

    // Set the error handler for E_ERROR to be the class handleError method.
    JError::setErrorHandling(E_ERROR, 'callback', array('plgSystemRedirect', 'handleError'));
}


static function handleError(&$error)
{
    // Get the application object.
    $app = JFactory::getApplication();

    // Make sure we are not in the administrator and it's a 404.
    if (!$app->isAdmin() and ($error->getCode() == 404))
    {
        // Do cool stuff here
    }
}

唯一的问题是 JError 已贬值,所以我不确定它何时会中断,例如它在 3.0、3.1、3.2 和 3.5 中应该没问题,但在那之后谁知道呢?

于 2013-01-08T05:41:19.330 回答
0

您可以使用以下技术执行此操作

检查网址功能

function checkURL($URL){
    $ch = curl_init($URL);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($httpcode != 200) {
        return false;
    }else{
        return true;
    }
}

CheckURL 函数的使用

/*URL May be a Joomla OR Non Joomla Site*/

if(checkURL("http://adidac.github.com/jmm/index.html")){
echo "URL Exist";
}else{
echo "URL NOT Exist";
//JError::raiseWarning(500,$URL. " is not exists");
}


if(checkURL("http://adidac.github.com/jmm/indexsdadssdasaasdaas.html")){
echo "URL Exist";
}else{
echo "URL NOT Exist";
//JError::raiseWarning(500,$URL. " is not exists");
}

注意:检查您是否安装了 PHP curl 库

于 2013-01-08T04:50:47.257 回答
0

我知道这是一个老问题,但我今天需要一个解决方案并找到了这个问题,所以将我的解决方案留在下面以帮助其他人在未来进行搜索。

在插件文件中:

/**
 * The global exception handler registered before the plugin was instantiated
 *
 * @var    callable
 * @since  3.6
 */
private static $previousExceptionHandler;

/**
 * Constructor.
 *
 * @param   object  &$subject  The object to observe
 * @param   array   $config    An optional associative array of configuration settings.
 *
 * @since   1.6
 */
public function __construct(&$subject, $config) {
    parent::__construct($subject, $config);
    
    // Register the previously defined exception handler so we can forward errors to it
    self::$previousExceptionHandler = set_exception_handler(array('PlgSystemVmsreporting', 'handleException'));
}

public static function handleException($exception) {
    // Wrap in try/catch to prevent any further exceptions being raised
    try {
        // Do whatever you need with the error
    } catch (Exception $e) {
        //Don't make a fuss - fail silently
    }
    
    // Proxy to the previous exception handler if available, otherwise use the default Joomla handler
    if (self::$previousExceptionHandler) {
        call_user_func_array(self::$previousExceptionHandler, array($exception));
    } else {
        ExceptionHandler::render($exception);
    }
    
}
于 2021-06-10T12:55:25.533 回答