Blame view

shops/application/core/route.php 1.58 KB
8d65d0ce   andryeyev   init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  <?php
  class Route
  {
      static function start()
      {
          $controller_name = 'Main';
          $action_name = 'index';
          
          $routes = explode('/shops/', $_SERVER['REQUEST_URI']);
  
          if ( !empty($routes[1]) )
          {	
              $controller_name = $routes[1];
          }
          
          if ( !empty($routes[2]) )
          {
              $action_name = $routes[2];
          }
  
          $model_name = 'Model_'.$controller_name;
          $controller_name = 'Controller_'.$controller_name;
          $action_name = 'action_'.$action_name;
  
          $model_file = strtolower($model_name).'.php';
          $model_path = "application/models/".$model_file;
          if(file_exists($model_path))
          {
              include "application/models/".$model_file;
          }
  
          $controller_file = strtolower($controller_name).'.php';
          $controller_path = "application/controllers/".$controller_file;
          if(file_exists($controller_path))
          {
              include "application/controllers/".$controller_file;
          }
          else
          {
              Route::ErrorPage404();
          }
          
          $controller = new $controller_name;
          $action = $action_name;
          
          if(method_exists($controller, $action))
          {
              $controller->$action();
          }
          else
          {
              Route::ErrorPage404();
          }
      
      }
      
      function ErrorPage404()
      {
          $host = 'http://'.$_SERVER['HTTP_HOST'].'/';
          header('HTTP/1.1 404 Not Found');
          header("Status: 404 Not Found");
          header('Location:'.$host.'404');
      }
  }