Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PHP OOP与现实世界用户的表单注册联系起来_Php_Forms_Validation_Oop - Fatal编程技术网

将PHP OOP与现实世界用户的表单注册联系起来

将PHP OOP与现实世界用户的表单注册联系起来,php,forms,validation,oop,Php,Forms,Validation,Oop,虽然我是PHP世界的新手,希望得到以下方面的帮助: 用户访问您的网站并填写表单后,用户将继续提交。一旦用户单击submit(使用PHP作为服务器框架),他们希望被重定向到他们的页面 现在,我如何将OOP新对象部分与网络的新用户关联起来?明白我的意思吗 我的面向对象方面: class Users { protected $firstname; protected $lastname; protected $password; protected $email; public function

虽然我是PHP世界的新手,希望得到以下方面的帮助:

用户访问您的网站并填写表单后,用户将继续提交。一旦用户单击submit(使用PHP作为服务器框架),他们希望被重定向到他们的页面

现在,我如何将OOP新对象部分与网络的新用户关联起来?明白我的意思吗

我的面向对象方面:

class  Users {
protected $firstname;
protected $lastname;
protected $password;
protected $email;


public function __construct($firstname, $lastname, $password, $email){
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->password = $firstname;
$this->email = $email;
}

//Some function here...

}

$newuser = new Users();
$anotheruser = new Users();
那么现在,在表单验证端之后,当新用户必须单击submit,并且他/她希望转到他们的页面时,我如何将表单验证端与我的OOP联系起来,以用于真实的流程

最后,新用户现在在另一个页面上看到welcome Mark(结果是:$this->firstname)


谢谢您……

您需要了解,除了定义
用户
类,这里还有很多事情要做

web应用程序的一般流程如下所示:

  • 用户在注册表中输入其数据
  • 表单将发布到php代码中
  • 应用程序类将POST请求路由到处理用户注册的特定代码
  • 验证用户数据,如果出现错误,则将错误响应发送回浏览器
  • 用户数据保存到数据库中
  • 用户会话已启动,因此您的应用程序知道他/她已登录
  • 成功响应发送到浏览器(或代码将浏览器重定向到配置文件页面)
  • 现在,在代码级别上,将有许多对象。 我假设您的应用程序将有一个入口点,即一些基本
    index.php
    文件,它接受所有请求,然后路由到处理程序

    因此,您将拥有(这不是真正的代码,只是一个结构):

    index.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    framework/Application.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    framework/Controller.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    application/controllers/UserController.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    application/models/User.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    依此类推,我们需要定义
    UserValidator
    UserTable
    以及其他所有内容

    因此,您的问题中的
    用户
    类只是大局的一小部分。 如果您想正确地执行OOP,您将拥有大量的类和对象。 它们可能与我上面描述的相似,也可能有很大的不同,但总体思路是相同的——您需要的功能应该被分割到一个相对较小的对象上,然后将它们组合起来构建应用程序


    如果您想这样做,最好的办法是学习如何在流行的框架中实现。您不需要从一开始就深入了解代码,只需浏览一些教程就可以了解框架用户的情况。

    您需要了解,除了定义
    用户
    类,这里还有很多事情要做

    web应用程序的一般流程如下所示:

  • 用户在注册表中输入其数据
  • 表单将发布到php代码中
  • 应用程序类将POST请求路由到处理用户注册的特定代码
  • 验证用户数据,如果出现错误,则将错误响应发送回浏览器
  • 用户数据保存到数据库中
  • 用户会话已启动,因此您的应用程序知道他/她已登录
  • 成功响应发送到浏览器(或代码将浏览器重定向到配置文件页面)
  • 现在,在代码级别上,将有许多对象。 我假设您的应用程序将有一个入口点,即一些基本
    index.php
    文件,它接受所有请求,然后路由到处理程序

    因此,您将拥有(这不是真正的代码,只是一个结构):

    index.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    framework/Application.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    framework/Controller.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    application/controllers/UserController.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    application/models/User.php

    $app = new WebApplication();
    $app->run();
    
    class WebApplication {
    
       public function run() {
           $request = new Request();  // will parse data from $_GET, $_POST, etc
           $router = new UrlRouter($request);
           $controller = $router->resolveRequest(); // according to the rules you define, it will find
                                                    // what controller to create
                                                    // controllers handle specific requests
           $controller->run();
       }
    
    }
    
    class Controller {   // You will have a base class for controllers
       // ...
    }
    
    class UserController extends Controller {   
    
       public function actionRegister() {
           // Create user validator and pass the Request object into it
           $validator = UserValidator($this->getRequest());
           if (!$validator->validate()) {
               // render the HTML with error
               $this->render('error', array('message'=>$validator->getError()));
               // OR send as json
               $this->sendJson(array(
                   'status'=>'error', 'message'=>$validator->getError()
               ));
           } else {
               $user = new User(
                   $this->getRequest()->get('email'),
                   $this->getRequest()->get('password')
                   // ...
               );
               $dbTable = new UsersTable();
               $dbTable->save($user);
               $this->startSession($user);
       }
    
    }
    
    class User extends Model {
    
        protected $email;
        protected $firstname;
        //... 
    
        public function __construct($email, $password) {
            // ...
        }
    
    }
    
    依此类推,我们需要定义
    UserValidator
    UserTable
    以及其他所有内容

    因此,您的问题中的
    用户
    类只是大局的一小部分。 如果您想正确地执行OOP,您将拥有大量的类和对象。 它们可能与我上面描述的相似,也可能有很大的不同,但总体思路是相同的——您需要的功能应该被分割到一个相对较小的对象上,然后将它们组合起来构建应用程序


    如果您想这样做,最好的办法是学习如何在流行的框架中实现。您不需要从一开始就深入了解代码,只需阅读几篇教程即可了解框架用户的情况。

    您可以使用会话变量将数据从一个步骤转发到另一个步骤:session_start();和$_SESSION['firstname']=$this->firstname;用户登录的位置。会话_start();并回应“欢迎”$_下一页的会话['firstname'],实际上这个问题太宽泛了,就像“我有一盒钉子,现在,我该如何建造房子?”。无论如何,我发布了我的答案,尽管它可能不是您期望看到的。您可以使用会话变量将数据从一个步骤转发到另一个步骤:session_start();和$_SESSION['firstname']=$this->firstname;用户登录的位置。会话_start();并回应“欢迎”$_下一页的会话['firstname'],实际上这个问题太宽泛了,就像“我有一盒钉子,现在,我该如何建造房子?”。无论如何,我发布了我的答案,尽管这可能不是你期望看到的。根据你的回答,你会建议我学习哪些框架来帮助我更好地构建我的应用程序?它们太多了,我似乎没有领先一步。谢谢你。@Louis我个人的偏好是Yii,Laravel似乎也很受欢迎。基于