私はJoomlaを開発しました!システムプラグイン。そのプラグインが実行されたときに間違ったURLを検出したいのですが。
例:
URL「http:// localhost / wrong-url」を入力すると、システムプラグインでそのエラーをキャッチしたいと思います。
システムがエラーページ(404)を表示することをどうやって知ることができますか?
私はJoomlaを開発しました!システムプラグイン。そのプラグインが実行されたときに間違ったURLを検出したいのですが。
例:
URL「http:// localhost / wrong-url」を入力すると、システムプラグインでそのエラーをキャッチしたいと思います。
システムがエラーページ(404)を表示することをどうやって知ることができますか?
をトラップするシステム プラグインでは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 では問題ないはずですが、その後は誰にもわかりません。
これは、次の手法を使用して行うことができます
URL機能の確認
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 lib がインストールされていることを確認してください