oop - Automated url request with php -
okay have been looking @ developing own custom cms system specific market , have been looking through few frameworks , problem have them don't automate routing. in laravel, if remember correctly respond url this:
route::get('/user', function() { return "this user"; }); this listen specific request. idea simplify create automated router. did setup .htaccess file took every request , directed index.php. took after .com, www.testsite.com/admin/pages/edit/5 , appended variable.
so in example above passing 4 parameters of single request:
admin - request path / used signify login check must done before passing them on request pages - class or object edit - method called class / object 5 - actual row of record in database being edited so developed router class looks this:
class router { public $url; public $protectedpaths = array('admin','users','client'); public function __construct() { $this -> url = explode('/', $_get['url']); if($this -> url[0] == '') { $this -> loaddefaultview(); } else { // check ensure path not protected if(in_array($this -> url[0], $this -> protectedpaths)) { // check ensure user logged in if($_cookie['islogged']) { // means there no action or model needed returned view if($this -> url[2] == '') { $this -> loadviewwithoutaction(); } else { // check ensure there controller if(file_exists(basecontrollers .'controller.'. $this -> url[1] .'.php')) { // require controller , instantiate require basecontrollers .'controller.'. $this -> url[1] .'.php'; $obj = new $this -> url[1]; // check see if method exists if(method_exists($obj, $this -> url[2])) { if($_post) { $data = $_post; } else { $data = array($this -> url[3]); } // run method if necessary $data = call_user_func_array(array($obj, $this -> url[2]), $data); $this -> loadadminview( $data ); } else { $this -> loaderrorview(); } } else { $this -> loaderrorview(); } } } else { header("location: /auth/form"); } } else { // check ensure there controller if(file_exists(basecontrollers .'controller.'. $this -> url[0] .'.php')) { // require controller , instantiate require basecontrollers .'controller.'. $this -> url[0] .'.php'; $obj = new $this -> url[0]; // check see if method exists if(method_exists($obj, $this -> url[1])) { // run method if necessary $data = call_user_func_array(array($obj, $this -> url[1]), array($this -> url[2])); $this -> loadpublicview( $data ); } else { $this -> loaderrorview(); } } else { $this -> loaderrorview(); } } } } so use number of different if else statements , perhaps switch differentiate between different requests etc. question, bad practice auto load class , run method. i've seen in frameworks, manual , i'm no expert i'm assuming there reason that. in addition, found little automating php requests in oop on web.
i'd automate @ same time not want cause security concerns. oh , forms or personal information being input users post or ajax protect against hacking url.
thanks advice or answers in advance!
Comments
Post a Comment