저는 현재 PHP CMS에서 메뉴 클래스를 리팩터링하고 있으며, 현재 누군가가 메뉴를 만들려고 할 때 (메뉴의 제목을 전달함으로써 문제를 처리하는 가장 좋은 방법을 찾으려고합니다. 바닥 글, 유틸리티 메뉴 등)을 가지고 있지만 메뉴가 데이터베이스에 없습니다.메뉴를 처리 할 수없는 가장 좋은 방법은 예외입니까?
찾을 수있는 메뉴가있는 메뉴 객체를 만들려고하면 문제가 없으므로 요청한대로 객체를 반환 할 수 있습니다. 그들은 찾을 수없는 하나를 만들려고, 나는 현재 전자 메일을 보내는 원인이 예외를 던지고있어, 다음 빈 메뉴 개체를 만들고 반환합니다. 출력 메뉴에 대한 호출이 다음 오류없이 작동하지만, 그러나 아무 것도 출력하지 않습니다.
(위의 설정은 메뉴 클래스의 정적 메소드는 메뉴 객체를 생성하기 위해 호출되며, 필요한 경우 예외를 던지거나 요청 된 메뉴 객체 또는 빈 객체를 반환 할 수 있습니다.
모두 의미가 있습니다. 이것이 가장 좋은 방법일까요? 아니면 좀 더 우아한 해결책이 있습니까?
크리스
편집 :
여기에 메뉴를 만들라고 정적 함수의 :
static function makeMenu($id,$breakDepth=1){
// try to create Menu
try {
$menu = new Menu($id, $breakDepth);
}
catch (no_menu_found_exception $e) {
// if we failed to find it, an email should have been sent, and create a blank menu so rest of site works without error
$menu = new Menu("");
}
return $menu;
}
가 여기에 생성자입니다 :
function __construct($id,$breakDepth=1){
$this->treeObject = Doctrine_Core::getTable('CmsMenuItemNew')->getTree();
if ($id == "") {
// if ID supplied is empty, return an empty menu object
$this->rootNode = null;
$this->name = $id;
return;
}
if (is_numeric($id)) {
// check it exists?
$this->rootNode = $id;
$node = Doctrine_Core::getTable('CmsMenuItemNew')->findByDQL("menuid = '".$id."'")->getFirst();
$this->name = $node->menutitle;
if ($this->name == "") $this->rootNode = null;
return;
} else {
$this->name = $id;
// find the menu ID for the supplied name
$table = Doctrine_Core::getTable('CmsMenuItemNew');
$table->setOption("orderBy", "level");
$this->rootNode = $table->findByDQL("menutitle = '$id'")->getFirst()->menuid;
// rootNode with supplied name not found, so look for a branch in the main menu
$this->breakDepth = $breakDepth;
if ($this->rootNode === null) {
throw new no_menu_found_exception("Menu not found: ".$id);
}
}
}
언급 한 바와 같이 - 그 아직 개발 중이므로 아직 완전히 완성되지 않았습니다.
메일을 보내어 예외를 던지고 처리하면 이미 꽤 우아하게 들립니다. 어쩌면 코드 스 니펫을 게시 할 수 있습니까? – Oli