Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Main app
- // asol routing eikhanei hobe
- class App
- {
- /********************************
- * url theke option gula vag hobe
- * projoniyo controller class e zabe
- * tar nirdisto method access korbe
- * method e params load hobe
- * e.g site/a/b/c
- * site/controller/method/params
- *********************************/
- // default controller and method
- protected $controller = 'home';
- protected $method = 'main';
- protected $params = array(); // empty array needed if no params
- /*********************************
- *** Construction
- **********************************/
- function __construct()
- {
- /****url ke vag kora shuru
- * store in $url
- ***************************/
- $url = $this->parseUrl();
- /*****************************************
- ** Checking options and calling controller
- ******************************************/
- if(!empty($url[0]) && ($url[0] != 'index.php'))
- {
- /******** controller ******/
- if(file_exists('../controller/'.$url[0].'.php'))
- {
- //controller found
- $this->controller = $url[0];
- unset($url[0]);
- //creating controller object
- require_once '../controller/'. $this->controller .'.php';
- $controller = new $this->controller;
- /******** Methods ***********/
- //checking methods
- if(!empty($url[1]))
- {
- if(method_exists( $controller, $url[1] ))
- { //method found
- $this->method = $url[1];
- unset($url[1]);
- /******** params ***********/
- //checking params
- if(!empty($url))
- { //params exists
- $this->params = array_values($url);
- echo "<br>params: ";
- print "<pre>";
- print_r($this->params);
- print "</pre>";
- }
- else //
- {
- echo "<br>no params";
- }
- $controller->{$this->method}();
- }
- else
- { //method not exists
- $this->method = "methodNotExists";
- require_once '../controller/errornf.php';
- $controller = new ErrorNF();
- $controller->{$this->method}();
- exit();
- }
- }
- else // method option not exists
- {
- $controller->{$this->method}();
- exit();
- }
- }
- else //No controller exists Error
- {
- require_once '../controller/errornf.php';
- $controller = new ErrorNF();
- $controller->{$this->method}();
- exit();
- }
- }
- else // No url , go default
- {
- include_once '../controller/'. $this->controller .'.php';
- $controller = new $this->controller;
- $controller->{$this->method}();
- exit();
- }
- } //end of __construct
- /**************************
- * Done with construct
- * finding options
- ****************************/
- public function parseUrl()
- {
- if(isset($_GET['url']))
- return $url = explode(
- '/',
- filter_var(
- rtrim( $_GET['url'], '/'),
- FILTER_SANITIZE_URL
- )
- );
- }
- } //end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement