将PHP OOP与现实世界用户的表单注册联系起来
虽然我是PHP世界的新手,希望得到以下方面的帮助: 用户访问您的网站并填写表单后,用户将继续提交。一旦用户单击submit(使用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
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应用程序的一般流程如下所示:
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应用程序的一般流程如下所示:
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似乎也很受欢迎。基于