Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]PHP framework tutorial - dziwna sprawa
darney
post
Post #1





Grupa: Zarejestrowani
Postów: 124
Pomógł: 0
Dołączył: 21.08.2010
Skąd: Gdynia

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


Witam ponownie. Uczę się tworzyć mały framework z fajnego przykładu

http://www.youtube.com/watch?v=ZbBf4jfwWko...;feature=relmfu

Cały Part 1 do pobrania

https://github.com/BaylorRae/PHP-MVC-Tutorial/tree/Part-1

Dlaczego napotykam błąd i nie tylko u mnie. Wyczytałem że już ktoś miał taki problem

Szlak mnie trafia że ludzie umieszczają tutoriale z błędem

Parse error: syntax error, unexpected T_STATIC in /home/xxx/ftp/moje_projekty/mvc/libraries/sammy.php on line 54

Ten post edytował darney 16.05.2011, 08:33:27
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
darney
post
Post #2





Grupa: Zarejestrowani
Postów: 124
Pomógł: 0
Dołączył: 21.08.2010
Skąd: Gdynia

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


Proszę bardzo : )

sammy.php
  1. <?php
  2. /**
  3.  * Sammy - A bare-bones PHP version of the Ruby Sinatra framework.
  4.  *
  5.  * @version 1.0
  6.  * @author Dan Horrigan
  7.  * @license MIT License
  8.  * @copyright 2010 Dan Horrigan
  9.  */
  10.  
  11. function get($route, $callback) {
  12. Sammy::process($route, $callback, 'GET');
  13. }
  14.  
  15. function post($route, $callback) {
  16. Sammy::process($route, $callback, 'POST');
  17. }
  18.  
  19. function put($route, $callback) {
  20. Sammy::process($route, $callback, 'PUT');
  21. }
  22.  
  23. function delete($route, $callback) {
  24. Sammy::process($route, $callback, 'DELETE');
  25. }
  26.  
  27. function ajax($route, $callback) {
  28. Sammy::process($route, $callback, 'XMLHttpRequest');
  29. }
  30.  
  31. class Sammy {
  32.  
  33. public static $route_found = false;
  34.  
  35. public $uri = '';
  36.  
  37. public $segments = '';
  38.  
  39. public $method = '';
  40.  
  41. public $format = '';
  42.  
  43. public static function instance() {
  44. static $instance = null;
  45.  
  46. if( $instance === null ) {
  47. $instance = new Sammy;
  48. }
  49.  
  50. return $instance;
  51. }
  52.  
  53. public static function run() {
  54. if( !self::$route_found ) {
  55. echo 'Route not defined!';
  56. }
  57.  
  58. }
  59.  
  60. public static function process($route, $callback, $type) {
  61. $sammy = self::instance();
  62.  
  63. // Check for ajax
  64. if( $type == 'XMLHttpRequest' )
  65. $sammy->method = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : 'GET';
  66.  
  67. if( self::$route_found || (!preg_match('@^'.$route.'(?:\.(\w+))?$@uD', $sammy->uri, $matches) || $sammy->method != $type) ) {
  68. return false;
  69. }
  70.  
  71. // Get the extension
  72. $extension = $matches[count($matches)-1];
  73. $extension_test = substr($sammy->uri, -(strlen($extension)+1), (strlen($extension)+1));
  74.  
  75. if( $extension_test == '.' . $extension )
  76. $sammy->format = $extension;
  77.  
  78. self::$route_found = true;
  79. echo $callback($sammy);
  80. }
  81.  
  82. public function __construct() {
  83. $this->uri = $this->get_uri();
  84. $this->segments = explode('/', trim($this->uri, '/'));
  85. $this->method = $this->get_method();
  86. }
  87.  
  88. public function segment($num) {
  89. $num--;
  90.  
  91. // Remove the extension
  92. $this->segments[$num] = isset($this->segments[$num]) ? rtrim($this->segments[$num], '.' . $this->format) : null;
  93.  
  94. return isset($this->segments[$num]) ? $this->segments[$num] : null;
  95. }
  96.  
  97. protected function get_method() {
  98. return isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
  99. }
  100.  
  101. protected function get_uri($prefix_slash = true) {
  102. if( isset($_SERVER['PATH_INFO']) ) {
  103. $uri = $_SERVER['PATH_INFO'];
  104. }elseif( isset($_SERVER['REQUEST_URI']) ) {
  105. $uri = $_SERVER['REQUEST_URI'];
  106.  
  107. if( strpos($uri, $_SERVER['SCRIPT_NAME']) === 0 ) {
  108. $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
  109. }elseif( strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0 ) {
  110. $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
  111. }
  112.  
  113. // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
  114. // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
  115. if( strncmp($uri, '?/', 2) === 0 ) {
  116. $uri = substr($uri, 2);
  117. }
  118.  
  119. $parts = preg_split('#?#i', $uri, 2);
  120. $uri = $parts[0];
  121.  
  122. if( isset($parts[1]) ) {
  123. $_SERVER['QUERY_STRING'] = $parts[1];
  124. parse_str($_SERVER['QUERY_STRING'], $_GET);
  125. }else {
  126. $_SERVER['QUERY_STRING'] = '';
  127. $_GET = array();
  128. }
  129. $uri = parse_url($uri, PHP_URL_PATH);
  130. }else {
  131. // Couldn't determine the URI, so just return false
  132. return false;
  133. }
  134.  
  135. // Do some final cleaning of the URI and return it
  136. return ($prefix_slash ? '/' : '').str_replace(array('//', '../'), '/', trim($uri, '/'));
  137. }
  138.  
  139. public function format($name, $callback) {
  140. $sammy = self::instance();
  141. if( !empty($sammy->format) && $name == $sammy->format )
  142. echo $callback($sammy);
  143. else
  144. return false;
  145. }
  146. }
  147.  
  148. $sammy = Sammy::instance();



router.php
  1. <?php
  2.  
  3. class Map {
  4.  
  5. public static function get($route, $callback) {
  6. Sammy::process($route, 'GET');
  7. }
  8.  
  9. public static function post($route, $callback) {
  10. Sammy::process($route, 'POST');
  11. }
  12.  
  13. public static function put($route, $callback) {
  14. Sammy::process($route, 'PUT');
  15. }
  16.  
  17. public static function delete($route, $callback) {
  18. Sammy::process($route, 'DELETE');
  19. }
  20.  
  21. public static function ajax($route, $callback) {
  22. Sammy::process($route, 'XMLHttpRequest');
  23. }
  24.  
  25. public static function dispatch() {
  26. // runs when find a matching route
  27.  
  28. // find the controller
  29.  
  30. // find the action
  31.  
  32. // include the app_controller
  33.  
  34. // include the matching route controller
  35.  
  36. // run the matching action
  37.  
  38. // include the view file
  39. }
  40.  
  41. }

index.php

  1. <?php
  2. //for use in development mode
  3. define ('BASE_PATH', dirname(realpath(__FILE__)) . '/');
  4.  
  5. define ('APP_PATH', BASE_PATH . 'app/'); //
  6.  
  7. //echo BASE_PATH;
  8.  
  9. include BASE_PATH . 'libraries/core.php';


Jestem pewien że kod jest na 99% dobrze napisany. Sprawdzałem do 4 nad ranem porównując z filmem oraz z gotowcem w zipie rozwiązaniem partu I .

Ten post edytował darney 16.05.2011, 10:29:25
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 14.10.2025 - 14:29