Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: MVC, problem z kontrolerem
Forum PHP.pl > Forum > PHP
Raven1122
Witam, mam problem z Kontrolerem MVC, otóż nie otrzymuje od niego żadnego odzewu ;s


router.php:

  1. <?php
  2.  
  3. class Router{
  4.  
  5. protected $baseURL;
  6. protected $parsedURL;
  7. protected $modelpath;
  8. protected $id;
  9. public $model;
  10.  
  11. public function __construct(){
  12. //Ustawienie ścieżki do modeli
  13. $this->baseURL = "/inc/models/";
  14. return $this->baseURL;
  15. }
  16.  
  17.  
  18. public function _parseURL($model){
  19. //Zapisanie tablicy z podzielonym adresem URL
  20. $this->parsedURL = explode('/', $model);
  21. if(ctype_alpha($this->parsedURL[1])){
  22. $this->modelpath = $this->baseURL . $this->parsedURL[1] . '.php';
  23.  
  24. if(array_key_exists(2, $this->parsedURL) && $this->parsedURL[2] != ''){
  25. $this->id = $this->parsedURL[2];
  26. $this->model = $this->parsedURL[2];
  27. return $this->id;
  28. return $this->modelpath;
  29. return $this->model;
  30. //Próba odzewu
  31. echo 'model';
  32.  
  33. }
  34. else{
  35. $this->id = null;
  36. $this->model = $this->parsedURL[2];
  37. return $this->id;
  38. return $this->modelpath;
  39. return $this->model;
  40. //Próba odzewu
  41. echo 'id';
  42. }
  43. }
  44. else{
  45. //header("Location:./index.php");
  46. }
  47. }
  48.  
  49.  
  50. public function _setRoute($model, $id){
  51. try{
  52. if(file_exist($this->modelpath)){
  53. if($id == null){
  54. include $this->modelpath;
  55. }
  56. else{
  57. include $this->modelpath.'?nid=' . $id;
  58. }
  59. }
  60. else{
  61. return false;
  62. }
  63. }
  64. catch(Exception $e){
  65. echo 'Caught exception:' . $e->getMessage();
  66. }
  67.  
  68. }
  69.  
  70.  
  71.  
  72.  
  73. }
  74.  
  75. ?>


Testowy plik model dla news:
  1. <?php
  2.  
  3. echo 'News';
  4. ?>


htaccess:
  1. RewriteEngine On
  2. RewriteCond %{REQUEST_FILENAME} !-f
  3. RewriteCond %{REQUEST_FILENAME} !-d
  4. RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php




No i wywolanie tego wszystkiego z linku http://testowyserwer.serwer.pl/news
  1. <?php
  2. function __autoload($classname){
  3. include strtolower($classname) . '.php';
  4. }
  5. $router = new Router();
  6. $router->_parseURL($_SERVER["REQUEST_URI"]);
  7. $router->_setRoute($model, $id);
  8. ?>



Po załadowaniu linku powyżej pokazuje się biała strona, nie wywołuje echo z news.php, ani z router.php
CuteOne
Sprawdź error logi php'a/apache. Pewnie palnąłeś gdzieś błąd
Spawnm
Jaki to ma sens?
  1. return $this->id;
  2. return $this->modelpath;
  3. return $this->model;


$model i $id nie istnieją.
  1. $router->_setRoute($model, $id);
Raven1122
Przeciez sa zdefiniowane w _parseURL i je zwracam

  1. public function _parseURL($model){
  2. //Zapisanie tablicy z podzielonym adresem URL
  3. $this->parsedURL = explode('/', $model);
  4. if(ctype_alpha($this->parsedURL[1])){
  5. $this->modelpath = $this->baseURL . $this->parsedURL[1] . '.php';
  6.  
  7. if(array_key_exists(2, $this->parsedURL) && $this->parsedURL[2] != ''){
  8. $this->id = $this->parsedURL[2];
  9. $this->model = $this->parsedURL[2];
  10. return $this->id;
  11. return $this->modelpath;
  12. return $this->model;
  13. //Próba odzewu
  14. echo 'model';
  15.  
  16. }
  17. else{
  18. $this->id = null;
  19. $this->model = $this->parsedURL[2];
  20. return $this->id;
  21. return $this->modelpath;
  22. return $this->model;
  23. //Próba odzewu
  24. echo 'id';
  25. }
  26. }
erix
Ale przecież pierwszy return przerywa Ci działanie funkcji...
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.