私はしばらく前に終了したフレームワークを更新しています。私はコミュニティからいくつかのフィードバックを得て、私のパッケージを格納するための少し移植性のあるMVCフレームワークを構築することに決めました。検証のためにPHP7で例外をスローしますか?
私は現在ルータで作業していますが、フレームワークに取得する予定のURLをフィードすると完全に動作しています。 URLをよりオブジェクト指向で、より簡単に検証するためにthrow new Exception("MyMessage")
を使用しました。
しかし、私はこの試みを初めて試みたことがあり、キャッチブロックがエラーをキャッチしていないようです。
具体的には、try
ブロックにエラーがスローされたときに、catch
ブロックにコードをトリガーする必要があります。ここで フルtry/catch
ブロックがあるが...
try
{
if (isset($url[0]) && isset($url[1]) && isset($url[2]) && $url[0] . $url[1] . $url[2] != "")
{
//Figure out if we're handling a package, or working in the package manager.
if ($url[0] != "ignition" && $url[0] != "")
{
if (!file_exists("../packages/" . $url[0]))
{
throw new Exception($manager_error = "Package \"" . $url[0] . "\" not found.");
}
else
{
$this->package = $url[0];
$this->path = "../packages/$this->package/controllers/";
}
unset($url[0]);
}
elseif ($url[0] == "")
throw new Exception("No package name provided.");
//Check if controller exists.
if ($url[1] != "")
{
//Check if controller file exists.
if (file_exists($this->path . $url[1] . ".php"))
{
$this->controller = $url[1];
require_once($this->path . "$this->controller.php");
unset($url[1]);
//Check that controller exists in file.
if (class_exists($this->controller))
{
$this->controller = new $this->controller;
//Check that method exists in controller.
if ($url[2] != "")
{
if (method_exists($this->controller, $url[2]))
{
$this->method = $url[2];
unset($url[2]);
if (!empty($url))
$this->params = array_values($url);
}
else
throw new Exception("Method \"" . $url[2] . "\" not found in controller \"" . get_class($this->controller) . "\".");
}
else
throw new Exception("No method name provided.");
}
else
throw new Exception("Controller \"$this->controller\" not contained in it's file.");
} else {
throw new Exception("Controller file \"" . $url[1] . ".php\" not found.");
}
}
else
throw new Exception("No controller name provided.");
}
else
{
//Check if we have one or more arguments, but less than the three
//we need for a complete route.
if (!isset($url[0]))
$url[0] = "";
if (!isset($url[1]))
$url[1] = "";
if (!isset($url[2]))
$url[2] = "";
if ($url[0] . $url[1] . $url[2] != "") {
if ($url[0] == "")
throw new Exception("No package name provided.");
elseif ($url[1] == "")
throw new Exception("No controller name provided.");
elseif ($url[2] = "")
throw new Exception("No method name provided.");
}
}
}
catch (Exception $e)
{
unset($url);
$this->manager_error = $e->getMessage();
$this->package = "ignition";
$this->controller = "ignition";
$this->method = "notFound";
$this->path = "../package_manager/controllers/";
$this->params[] = $this->manager_error;
require_once($this->path . "$this->controller.php");
$this->controller = new $this->controller;
}
私はちょうどいくつかのテキストをエコー出力し、試してみて、キャッチから何かを得るためにアドレスバーにいくつかの間違ったURLを入れた非常に単純なバージョンを作ってみました私は成功していません。
これは、クラスのコンストラクタ関数内にあることを言いたいと思います。
私はここで間違っていますが、どのように例外を捕まえるためにこのスクリプトを変更できますか?
例外がスローされている箇所でコードを見ることなくこれに答えるのは難しいです。 –
私はあなたが望むならば、スクリプト全体を投稿することができます。条件付きで例外がスローされる限り、スローされる場所は問題になりますか? – Allenph
少なくとも、私たちの助けを必要としている完全な 'try/catch'コードを表示してください。 –