Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [ZendFramework]Nowy error code
lolkiewicz
post
Post #1





Grupa: Zarejestrowani
Postów: 37
Pomógł: 0
Dołączył: 12.03.2012

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


Witam,

mam taki problem, chciałem dodać obsługę błędów w mojej aplikacji i nie do końca wiem jak mam to zrobić bo w necie nie specjalnie o tym mowa.
Mam sobie następującą funkcję z ErrorController.
  1. public function errorAction() {
  2. $this->view->headLink()->appendStylesheet($this->view->baseUrl('/css/default/default.css'));
  3. $this->view->headLink()->appendStylesheet($this->view->baseUrl('/css/default/error.css'));
  4. if ($this->_request->isXmlHttpRequest())
  5. $this->_helper->getHelper('layout')->disableLayout();
  6.  
  7. $errors = $this->_getParam('error_handler');
  8.  
  9. switch ($errors->type) {
  10.  
  11. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
  12. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  13. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  14.  
  15. // 404 error -- controller or action not found
  16. $this->disableLayoutHTTP();
  17. $this->getResponse()->setHttpResponseCode(404);
  18. $this->renderScript('/error/404.phtml');
  19. break;
  20.  
  21. default:
  22.  
  23. // application error
  24. $this->disableLayoutHTTP();
  25. $this->getResponse()->setHttpResponseCode(500);
  26. $this->renderScript('/error/500.phtml');
  27. break;
  28. }
  29.  
  30. // Log exception, if logger available
  31. if ($log = $this->getLog()) {
  32. $log->crit($this->view->message, $errors->exception);
  33.  
  34. }
  35.  
  36. // conditionally display exceptions
  37. if ($this->getInvokeArg('displayExceptions') == true) {
  38. $this->view->exception = $errors->exception;
  39. }
  40.  
  41. $this->view->request = $errors->request;
  42. }


I co mam teraz zrobić aby dodać np 403 lub 401 ? Bo ogólnie nie wiem nawet jak się za to zabrać, próbowałem na wiele sposobów ale nic mi nie wychodzi. Jak ktoś mia jakiś pomysł to niech pisze. Każda sensowna myśl lepsza niż jej brak.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
Fluke
post
Post #2





Grupa: Zarejestrowani
Postów: 247
Pomógł: 9
Dołączył: 20.09.2010
Skąd: Kraków

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


No masz już tam zrobione wszystko. Jedynie musisz stworzyć widok /error/404.phtml. W zależności od tego jaki przesyłasz exception taki wyświetlasz komunikat.
Go to the top of the page
+Quote Post
lolkiewicz
post
Post #3





Grupa: Zarejestrowani
Postów: 37
Pomógł: 0
Dołączył: 12.03.2012

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


Cytat(Fluke @ 26.11.2013, 08:49:29 ) *
No masz już tam zrobione wszystko. Jedynie musisz stworzyć widok /error/404.phtml. W zależności od tego jaki przesyłasz exception taki wyświetlasz komunikat.


Albo Ty nie przeczytałeś tego co było pod kodem albo po prostu ja nie rozumiem co ma wspólnego tworzenie widoku do 404 z obsługą błędu 403 ? Jesteś mi to w stanie wytłumaczyć ?
Go to the top of the page
+Quote Post
RiE
post
Post #4





Grupa: Zarejestrowani
Postów: 97
Pomógł: 45
Dołączył: 5.05.2010

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


Nie siedziałem za długo w Zend Framework 1, więc moje rozwiązanie może być łopatologiczne, ale szedłbym w tym kierunku:
Zrobił klasę dziedziczącą od Exception, powiedzmy Custom_Exception_Forbidden, następnie w momencie zgłoszenia błędu korzystałbym z tej klasy w stylu:
  1. throw new Custom_Exception_Forbidden('Dostęp wzbroniony');


I teraz w ErrorAction:


  1. switch ($errors->type) {
  2.  
  3. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
  4. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  5. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  6.  
  7. // 404 error -- controller or action not found
  8. $this->disableLayoutHTTP();
  9. $this->getResponse()->setHttpResponseCode(404);
  10. $this->renderScript('/error/404.phtml');
  11. break;
  12.  
  13. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
  14. // 403 error
  15. if($errors->exception instanceof Custom_Exception_Forbidden) {
  16. $this->getResponse()->setHttpResponseCode(403);
  17. $this->view->error_message = 'Twój komunikat' // Ewentualnie to $errors->exception->getMessage() powinno zwrócić wiadomość z kontrolera klasy
  18. break;
  19. }
  20.  
  21. default:
  22.  
  23. // application error
  24. $this->disableLayoutHTTP();
  25. $this->getResponse()->setHttpResponseCode(500);
  26. $this->renderScript('/error/500.phtml');
  27. break;
  28. }


Ten post edytował RiE 28.11.2013, 17:47:13
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 19.08.2025 - 16:27