Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Błędy i wyjątki
by_ikar
post
Post #1





Grupa: Zarejestrowani
Postów: 1 798
Pomógł: 307
Dołączył: 13.05.2009
Skąd: Gubin/Wrocław

Ostrzeżenie: (0%)
-----


Hej, możliwe że tytuł tematu jest mylny, ale nie wiedziałem w gruncie jak mam opisać w kilku słowach swój problem.
Tak więc, przechodząc do meritum - od kilku dni piszę sobie swojego "CMS'a" na nowo. Nie jest to nic skomplikowanego, a musi być lekkie, dlatego nie użyłem żadnego framework'a typu kohana czy zend. Z tym że staram się wzorować na kohanie, nie wszystko oczywiście, ale cześć. Napisałem klasę odpowiedzialną za routing (router), z tą różnicą w porównaniu do kohany, że tam adres jest pobierany poprzez QUERY_STRING a u mnie REQUEST_URI. Mój problem polega na tym, że za bardzo nie wiem w jaki sposób, żeby to wyglądało elegancko, zainicjować działanie routera. Chodzi o to, żeby przy nie prawidłowym adresie, wyświetlić stronę błędu, a mi to niestety nie wychodzi (IMG:style_emoticons/default/biggrin.gif) wcześniej robiłem to tak, że sprawdzałem sobie (in_array) czy dany adres (po rozbiciu oczywiście) znajduje się w tablicy routings. W przypadku nie znalezienia redirect do strony błędu. Nie wiedziałem wcześniej że jest możliwość wyświetlenia poprzez (chyba) klasę Exception (chyba ponieważ z tej klasy dziedziczy Kohana_Exception). Próbowałem napisać jakąś własną obsługę wyjątków/błędów, ale niestety marnie mi to wychodzi. Więc jeżeli jest ktoś, kto ogarnął to co napisałem w chaotyczny sposób, i byłby w stanie mi wytłumaczyć jak mam się za to za brać, to byłbym wdzięczny (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
by_ikar
post
Post #2





Grupa: Zarejestrowani
Postów: 1 798
Pomógł: 307
Dołączył: 13.05.2009
Skąd: Gubin/Wrocław

Ostrzeżenie: (0%)
-----


Okej, jest to jakieś wyjście, ale mnie chodzi trochę o coś innego. W kohanie nie ma (tak mi się wydaje, ponieważ nie znalazłem) plików odpowiedzialnych za stronę błędu, a korzysta z tych które są w apache. Przykładowo pokażę jak to wygląda w kohanie:

  1. // Include the Controller file
  2. require Router::$controller_path;
  3.  
  4. try
  5. {
  6. // Start validation of the controller
  7. $class = new ReflectionClass(ucfirst(Router::$controller).'_Controller');
  8. }
  9. catch (ReflectionException $e)
  10. {
  11. // Controller does not exist
  12. Event::run('system.404');
  13. }


klasa Kohana_404_Exception:

  1. /**
  2.  * Creates a Page Not Found exception.
  3.  */
  4. class Kohana_404_Exception extends Kohana_Exception {
  5.  
  6. protected $code = E_PAGE_NOT_FOUND;
  7.  
  8. /**
  9. * Set internal properties.
  10. *
  11. * @param string URL of page
  12. * @param string custom error template
  13. */
  14. public function __construct($page = FALSE, $template = FALSE)
  15. {
  16. if ($page === FALSE)
  17. {
  18. // Construct the page URI using Router properties
  19. $page = Router::$current_uri.Router::$url_suffix.Router::$query_string;
  20. }
  21.  
  22. Exception::__construct(Kohana::lang('core.page_not_found', $page));
  23.  
  24. $this->template = $template;
  25. }
  26.  
  27. /**
  28. * Sends "File Not Found" headers, to emulate server behavior.
  29. *
  30. * @return void
  31. */
  32. public function sendHeaders()
  33. {
  34. // Send the 404 header
  35. header('HTTP/1.1 404 File Not Found');
  36. }
  37.  
  38. } // End Kohana 404 Exception


I klasa Kohana_Exception:

  1. /**
  2.  * Creates a generic i18n exception.
  3.  */
  4. class Kohana_Exception extends Exception {
  5.  
  6. // Template file
  7. protected $template = 'kohana_error_page';
  8.  
  9. // Header
  10. protected $header = FALSE;
  11.  
  12. // Error code
  13. protected $code = E_KOHANA;
  14.  
  15. /**
  16. * Set exception message.
  17. *
  18. * @param string i18n language key for the message
  19. * @param array addition line parameters
  20. */
  21. public function __construct($error)
  22. {
  23. $args = array_slice(func_get_args(), 1);
  24.  
  25. // Fetch the error message
  26. $message = Kohana::lang($error, $args);
  27.  
  28. if ($message === $error OR empty($message))
  29. {
  30. // Unable to locate the message for the error
  31. $message = 'Unknown Exception: '.$error;
  32. }
  33.  
  34. // Sets $this->message the proper way
  35. parent::__construct($message);
  36. }
  37.  
  38. /**
  39. * Magic method for converting an object to a string.
  40. *
  41. * @return string i18n message
  42. */
  43. public function __toString()
  44. {
  45. return (string) $this->message;
  46. }
  47.  
  48. /**
  49. * Fetch the template name.
  50. *
  51. * @return string
  52. */
  53. public function getTemplate()
  54. {
  55. return $this->template;
  56. }
  57.  
  58. /**
  59. * Sends an Internal Server Error header.
  60. *
  61. * @return void
  62. */
  63. public function sendHeaders()
  64. {
  65. // Send the 500 header
  66. header('HTTP/1.1 500 Internal Server Error');
  67. }
  68.  
  69. } // End Kohana Exception


I z tych dwóch klas i tego kawałka kodu nie potrafię wywnioskować, w jaki sposób gdy router nie znajdzie kontrolera wywala 404 page not found :| ale okej, zrobię w taki sposób na który mnie naprowadziłeś, dzięki (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 16.10.2025 - 19:48