How to load classes based on pretty URLs in MVC-like page?

后端 未结 2 1852
无人及你
无人及你 2020-11-22 10:33

I would like to ask for some tips, on how solve this problem. I\'m trying to build my own MVC website. I learned the basics of the URL.

http://example.com/bl         


        
2条回答
  •  庸人自扰
    2020-11-22 11:23

    I think you are over thinking this one....

    So the controller/resource is a blog....the method that all of them should run from is "read" (using crud...I usually call it data but basically its your select). Now just have your method accept a category value that automatically gets mapped based on the url....

    here is a sample...completely untested but just to show you the idea (using pdo)

    public function data($id = 0, $category = 0){
        if (isset($id) AND $id != 0){
             $bind = array(":id", $id);
             $results = $db->query("SELECT * FROM blog WHERE blog_id = :id", $bind);
             return $results[0];
        } else if (isset($category) AND $id != 0){ 
             $approved_categories = array("cosplay","game","movie","series");
             if (in_array($category, $approved_categories)){
                  $bind = array(":cat", $category);
                  $results = $db->query("SELECT * FROM blog WHERE blog_cat = :cat", $bind);
             } 
             return $results;
        }
    }
    

提交回复
热议问题