0 Replies - 8644 Views - Last Post: 09 February 2013 - 10:31 AM

#1 been_1990  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 20-February 10

Basic Url Router - simliar to Laravel's

Posted 09 February 2013 - 10:31 AM

I've implemented a very basic routing class that follows the ease-of-use Laravel routing system.
I want feedback on what could be wrong, completely ridiculous or downright terrible:

router.php:
class Router {

	protected static $_instance;
	private $url;
	private $_404;

	function __construct($uri){
		self::$_instance = $this;
		$this->url = $uri;
		$this->_404 = true;		
	}

	public function __destruct()
    {
        if($this->_404){
        	echo 'Can\'t find page';
        }
    }

	public static function get($uri,$function){		
		$o = self::$_instance;
		if($_SERVER['REQUEST_METHOD'] != "GET") return;

		if( $o instanceof Router ) {
            if(self::$_instance->url == $uri){
            	$o->_404 = false;
            	$function();
            }
        }
	}

	public static function post($uri,$function){
		$o = self::$_instance;

		if($_SERVER['REQUEST_METHOD'] != "POST") return;

		if( $o instanceof Router ) {
            if(self::$_instance->url == $uri){
            	$o->_404 = false;
            	$function();
            }
        }
	}

	public static function put($uri,$function){
		$o = self::$_instance;

		if($_SERVER['REQUEST_METHOD'] != "PUT") return;

		if( $o instanceof Router ) {
            if(self::$_instance->url == $uri){
            	$o->_404 = false;
            	$function();
            }
        }
	}

	public static function delete($uri,$function){
		$o = self::$_instance;

		if($_SERVER['REQUEST_METHOD'] != "DELETE") return;

		if( $o instanceof Router ) {
            if(self::$_instance->url == $uri){
            	$o->_404 = false;
            	$function();
            }
        }
	}

	public static function any($function){
		$o = self::$_instance;

		if( $o instanceof Router ) {
            $function();
        }
	}

	public static function url($path,$get=null){
		$g = ($get) ? '/?'.$get : '';
		return BASE_URL.'/index.php/'.$path.$g;
	}

	public static function e_url($path,$get=null){
		$g = ($get) ? '/?'.$get : '';
		echo BASE_URL.'/index.php'.$path.$g;
	}

	public static function current(){
		$o = self::$_instance;

		if( $o instanceof Router ) {
            return $o->url;
        }

        return null;
	}

	public static function asset($path){
		return BASE_URL.'/public/'.$path;
	}

	public static function e_asset($path){
		echo BASE_URL.'/public/'.$path;
	}

	public static function thumb($thumb){
		if (trim($thumb) == '') {
			$thumb = 'default.png';
		}
		return BASE_URL.'/public/images/thumbnails/'.$thumb;
	}

	public static function e_thumb($thumb){
		if (trim($thumb) == '') {
			$thumb = 'default.png';
		}
		echo BASE_URL.'/public/images/thumbnails/'.$thumb;
	}

	public static function redirect($path){
		header("Location: ".BASE_URL.'/index.php/'.$path);
		exit();
	}

	public static function flash($data=null)
	{
		if ($data != null) {
			$_SESSION['flash_dancing'] = $data;
		}else{
			return $_SESSION['flash_dancing'];
		}
	}
}



I the use it on index.php:
$uri = explode('/', isset($_SERVER["PATH_INFO"]) ? $_SERVER["PATH_INFO"] : '');
array_shift($uri);
$route = '/'.implode('/', $uri);
$route = rtrim($route, '/');

$router = new Router($route);

include "application/routes.php"; //file that will contain code for each route, I have not made controllers available yet



routes.php:

Router::get('/', function(){

	$db = DB::DB(Connection::connect(DBHOST,DB,DBUSER,DBPASS));
	$posts = $db->posts();

	View::make('home', array( 'posts' => $posts));

});

Router::delete('/delete-something-ajax', function(){

	/* ... */

        parse_str(file_get_contents("php://input"),$delete_vars);

	die(json_encode( 'message' => $sie_message));

});



A lot of places on the web say using singletons and static functions are bad, but I absolutely love Laravel's routing system, and I really liked this quick solution I've come up with.

Feel free to point out flaws, especially if there are security ones.

Thanks.

This post has been edited by been_1990: 09 February 2013 - 10:45 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1