An example of an MVC controller

后端 未结 6 547
有刺的猬
有刺的猬 2020-12-22 16:09

I have been reading a lot about how and why to use an MVC approach in an application. I have seen and understand examples of a Model, I have seen and understand examples of

6条回答
  •  半阙折子戏
    2020-12-22 16:57

    uri;
        }
    
        /**
         * 
         * @return mixed
         */
        function getController() {
            return $this->controller;
        }
    
        /**
         * 
         * @return mixed
         */
        function getAction() {
            return $this->action;
        }
    
        /**
         * 
         * @return mixed
         */
        function getParams() {
            return $this->params;
        }
    
        function getRoute() {
            return $this->route;
        }
    
        function getMethodPrefix() {
            return $this->method_prefix;
        }
    
            public function __construct($uri) {
                $this->uri = urldecode(trim($uri, "/"));
                //defaults
                $routes = Config::get("routes");
                $this->route = Config::get("default_route");
                $this->controller = Config::get("default_controller");
                $this->action = Config::get("default_action");
                $this->method_prefix= isset($routes[$this->route]) ? $routes[$this->route] : '';
    
    
                //get uri params
                $uri_parts = explode("?", $this->uri);
                $path = $uri_parts[0];
                $path_parts = explode("/", $path);
    
                if(count($path_parts)){
                    //get route
                    if(in_array(strtolower(current($path_parts)), array_keys($routes))){
                        $this->route = strtolower(current($path_parts));
                        $this->method_prefix = isset($routes[$this->route]) ? $routes[$this->route] : '';
                        array_shift($path_parts);
                    }
    
                    //get controller
                    if(current($path_parts)){
                        $this->controller = strtolower(current($path_parts));
                        array_shift($path_parts);
                    }
    
                    //get action
                    if(current($path_parts)){
                        $this->action = strtolower(current($path_parts));
                        array_shift($path_parts);
                    }
    
                    //reset is for parameters
                    //$this->params = $path_parts;
                    //processing params from url to array
                    $aParams = array();
                    if(current($path_parts)){ 
                        for($i=0; $iparams = (object)$aParams;
                }
    
        }
    }
    

提交回复
热议问题