Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

model view controller - handling forms in PHP MVC architecture

I'm trying to learn MVC architecture in PHP. this is what I've got so far: (there are more views such as login,...)

enter image description here

this is my Database.php :

class Database {

    public static $host="127.0.0.1";
    public static $dbName= "db6";
    public static $username = "username";
    public static $password = "password";

    private static function connect() {
            $pdo = new PDO("mysql:host=".self::$host.";dbname=".self::$dbName.";charset=utf8", self::$username, self::$password);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $pdo;
    }

    public static function query($query, $params = array()) {
            $statement = self::connect()->prepare($query);
            $statement->execute($params);

            if (explode(' ', $query)[0] == 'SELECT') {
            $data = $statement->fetchAll();
            return $data;
            }

            
    }

}

and Controller.php:

class Controller extends Database {

public static function CreateView($viewName){
    require_once("./view/$viewName.php");
    static::doSomething();
}

}

and Route.php :

class Route {
public static $validRoutes = array();

public static function set($route, $function){
    self::$validRoutes[] = $route;
    //print_r(self::$validRoutes);
    if($_GET['url'] == $route) {
        $function->__invoke();
    }

}

}

in view/login.php I have a form for user login:

<form _lpchecked="1" method="POST">
      <div class="form-group">
         User Name:<input type="text" class="form-control" id="loguser" placeholder="User Name" name="loguser">
      </div>                       
      <div class="form-group">
        Password:<input type="password" class="form-control" id="password" placeholder="Password" name="logpass">
      </div>
      <div class="d-flex flex-row align-items-center justify-content-between">
        <a href="jhl.php">Cancel</a>
        <button class="btn btn-primary">Login</button>
      </div>
   </form>

How can I handle the form? and also how can I complete my db class since it's only working for SELECT queries? thanks inadvance

question from:https://stackoverflow.com/questions/65940976/handling-forms-in-php-mvc-architecture

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...