Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Błędy i wyjątki
Forum PHP.pl > Forum > Przedszkole
by_ikar
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 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 smile.gif
marcio
  1. <?php
  2.  
  3. class Dispatcher {
  4.  
  5.  
  6. private $errors = array();
  7.  
  8.  
  9. public function Exec(Router $router) {
  10.  
  11. if(file_exists($router -> cfg -> PathControllers.$router -> getController().'.php')) {
  12.  
  13. require_once($router -> cfg -> PathControllers.$router -> getController().'.php');
  14.  
  15. $AppController = $router -> getController();
  16. $AppAction = $router -> getAction();
  17.  
  18. $obj = new $AppController();
  19. $obj -> $AppAction();
  20.  
  21. }
  22.  
  23. else {
  24.  
  25. Controller::Error(404);
  26.  
  27. }
  28.  
  29. }
  30.  
  31. }
  32.  
  33. ?>

Gdzie metoda statyczna Error() z klasy Controller zwraca blad tak:
  1. public static function Error($error) {
  2.  
  3. if(file_exists('views/'.$error.'.php')) {
  4.  
  5. if($error == 404) {
  6.  
  7. die(file_get_contents('views/'.$error.'.php'));
  8.  
  9. }
  10.  
  11. else {
  12.  
  13. die(file_get_contents('views/'.$error.'.php'));
  14.  
  15. }
  16.  
  17. }
  18.  
  19. }
by_ikar
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 smile.gif
nemis
Kohana zdecydowanie MA swoja obsluge 404. Nie jestem w stanie Ci teraz podac gdzie dokladnie, ale przejrzyj sobie plik Kohana.php z system.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.