Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Zaimplementowanie tras routingu
Forum PHP.pl > Forum > PHP > Object-oriented programming
walus16
Witam, napisałem kod Front controllera oraz Routera i chciałbym jeszcze do tego dodać tablice routingu. Znalazłem na GitHubie (https://github.com/simonhamp/routes) rozwiązanie, które chciałbym dodać do mojego kodu ale kompletnie mi to nie wychodzi.

Front Controller
  1. <?php
  2.  
  3. require_once SYS_PATH . 'core/controller.php';
  4. require_once SYS_PATH . 'core/router.php';
  5.  
  6. class Bootstrap
  7. {
  8. private $controller;
  9. private $action;
  10.  
  11. public function init(Router $request)
  12. {
  13. $controller = $request->getController();
  14. $method = $request->getMethod();
  15. $args = $request->getArgs();
  16.  
  17.  
  18. $controllerFile = APP_PATH. 'controllers/'.$controller.'.php';
  19.  
  20. if(is_readable($controllerFile)){
  21. require_once $controllerFile;
  22.  
  23. $controller = new $controller;
  24. $method = (is_callable(array($controller,$method))) ? $method : 'index';
  25.  
  26. if(!empty($args)){
  27. call_user_func_array(array($controller,$method),$args);
  28. }else{
  29. call_user_func(array($controller,$method));
  30. }
  31. return;
  32. }
  33. }
  34. }


Router
  1. <?php
  2.  
  3. class Router
  4. {
  5. private $controller;
  6. private $action;
  7. private $args;
  8.  
  9. public function __construct()
  10. {
  11. $path_info = !empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : (!empty($_SERVER['ORIG_PATH_INFO']) ? $_SERVER['ORIG_PATH_INFO'] : '');
  12. $parts = explode('/', $path_info);
  13. $parts = array_filter($parts);
  14.  
  15. $this->controller = ($c = array_shift($parts)) ? $c : 'welcome';
  16. $this->action = ($c = array_shift($parts)) ? $c : 'index';
  17. $this->args = (isset($parts[0])) ? $parts : array();
  18. }
  19.  
  20. public function getController()
  21. {
  22. return $this->controller;
  23. }
  24.  
  25. public function getMethod()
  26. {
  27. return $this->action;
  28. }
  29.  
  30. public function getArgs()
  31. {
  32. return $this->args;
  33. }
  34. }
Pyton_000
Szklana kula mnie się zepsuła sad.gif
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.