Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Zaimplementowanie tras routingu
walus16
post 1.10.2014, 07:33:14
Post #1





Grupa: Zarejestrowani
Postów: 30
Pomógł: 0
Dołączył: 24.05.2011

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


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. }
Go to the top of the page
+Quote Post

Posty w temacie
- walus16   Zaimplementowanie tras routingu   1.10.2014, 07:33:14
- - Pyton_000   Szklana kula mnie się zepsuła   2.10.2014, 09:18:08


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 Wersja Lo-Fi Aktualny czas: 14.08.2025 - 05:39