Advertisement
HoneyRemedy

Untitled

Aug 22nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. <?php
  2. //Main app
  3. // asol routing eikhanei hobe
  4.  
  5.  
  6. class App
  7. {
  8.     /********************************
  9.     * url theke option gula vag hobe
  10.     * projoniyo controller class e zabe
  11.     * tar nirdisto method access korbe
  12.     * method e params load hobe
  13.     * e.g site/a/b/c
  14.     * site/controller/method/params
  15.     *********************************/
  16.  
  17.     // default controller and method
  18.     protected $controller = 'home';
  19.     protected $method = 'main';
  20.     protected $params = array(); // empty array needed if no params
  21.    
  22.    /*********************************
  23.    *** Construction
  24.    **********************************/
  25.     function __construct()
  26.     {
  27.         /****url ke vag kora shuru
  28.         * store in $url
  29.         ***************************/
  30.         $url = $this->parseUrl();
  31.  
  32.  
  33.         /*****************************************
  34.         ** Checking options and calling controller
  35.         ******************************************/
  36.         if(!empty($url[0]) && ($url[0] != 'index.php'))
  37.         {  
  38.                 /******** controller ******/
  39.             if(file_exists('../controller/'.$url[0].'.php'))
  40.             {  
  41.                 //controller found
  42.                 $this->controller = $url[0];   
  43.                 unset($url[0]);
  44.  
  45.                 //creating controller object
  46.                 require_once '../controller/'. $this->controller .'.php';
  47.                 $controller = new $this->controller;
  48.  
  49.                 /******** Methods ***********/
  50.                 //checking methods
  51.                 if(!empty($url[1]))
  52.                 {  
  53.                     if(method_exists( $controller, $url[1] ))
  54.                     {   //method found
  55.                         $this->method = $url[1];
  56.                         unset($url[1]);
  57.  
  58.                         /******** params ***********/
  59.                         //checking params
  60.                         if(!empty($url))
  61.                             {   //params exists
  62.                                 $this->params = array_values($url);
  63.                                 echo "<br>params: ";
  64.                                 print "<pre>";
  65.                                 print_r($this->params);
  66.                                 print "</pre>";
  67.                             }
  68.                             else //
  69.                                 {
  70.                                     echo "<br>no params";
  71.                                 }
  72.  
  73.                         $controller->{$this->method}();
  74.  
  75.                     }
  76.                     else
  77.                         {  //method not exists
  78.                             $this->method = "methodNotExists";
  79.                             require_once '../controller/errornf.php';
  80.                             $controller = new ErrorNF();
  81.                             $controller->{$this->method}();
  82.                             exit();
  83.                         }
  84.                 }
  85.                 else // method option not exists
  86.                      { 
  87.                         $controller->{$this->method}();
  88.                         exit();
  89.                      }         
  90.             }
  91.             else //No controller exists Error
  92.                 {      
  93.                     require_once '../controller/errornf.php';
  94.                     $controller = new ErrorNF();
  95.                     $controller->{$this->method}();
  96.                     exit();
  97.                 }
  98.  
  99.  
  100.         }
  101.         else // No url , go default
  102.             {
  103.                 include_once '../controller/'. $this->controller .'.php';
  104.                 $controller = new $this->controller;
  105.                 $controller->{$this->method}();
  106.                 exit();
  107.             }
  108.  
  109.  
  110.  
  111.     } //end of __construct
  112.  
  113.     /**************************
  114.     * Done with construct
  115.     * finding options
  116.     ****************************/
  117.     public function parseUrl()
  118.     {
  119.        
  120.         if(isset($_GET['url']))
  121.             return $url = explode(
  122.                                   '/',
  123.                                   filter_var(
  124.                                              rtrim( $_GET['url'], '/'),
  125.                                              FILTER_SANITIZE_URL
  126.                                             )
  127.                                  );
  128.     }
  129.  
  130.    
  131. } //end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement