オブジェクト指向の勉強を始めました。しかし、イマイチよくわかりません。
コーディングを教えてくれる動画を観ながら勉強しています。
その動画では、
フォームから値をとる→入力チェック(OK)→データベースに格納→ログイン
フォームから値をとる→入力チェック(NG)→フォームに戻る
というロジックで教えています。
もし、
フォームから値をとるsignup.php→入力チェックS(OK)→データベースに格納→ログイン
の部分を、
フォームから値をとるsignup.php→入力チェック(OK)→確認画面check.php(OK)→データベースに格納→ログイン
のように、ひとつ工程を加えたいのですが、どのようにすれば値をcheck.phpに渡すことができるのでしょうか。
■signup.php
// 新規登録 require_once(__DIR__ . '/../config/config.php'); $app = new MyApp\Controller\Signup(); $app -> run(); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>Sign Up</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div id="container"> <form action="" method="post" id="signup"> <p><input type="text" name="email" placeholder="email" value="<?= isset($app->getValues()->email) ? $app->getValues()->email : '' ;?>"></p> <p class="err"><?= h($app->getErrors('email')) ?></p> <p><input type="password" name="password" placeholder="password"></p> <p class="err"><?= h($app->getErrors('password')) ?></p> <div class="btn" onclick="document.getElementById('signup').submit();">Sign Up</div> <p class=fs12""><a href="login.php">Log In</a></p> <input type="hidden" name="token" value="<?= h($_SESSION['token']); ?>"> </form> </div> </body> </html> コード
■Controller.php
namespace MyApp; class Controller{ private $_errors; private $_values; public function __construct(){ if (!isset($_SESSION['token'])) { $_SESSION['token'] = bin2hex(openssl_random_pseudo_bytes(16)); } $this->_errors = new \stdClass(); $this->_values = new \stdClass(); } protected function setErrors($key, $error){ $this->_errors->$key = $error; } public function getErrors($key){ return isset($this->_errors->$key) ? $this->_errors->$key : ""; } protected function setValues($key, $value){ $this->_values->$key = $value; } public function getValues(){ return $this->_values; } protected function hasError(){ return !empty(get_object_vars($this->_errors)); } protected function isLoggedIn(){ return isset($_SESSION['me']) && !empty($_SESSION['me']); } public function me(){ return $this->isLoggedIn() ? $_SESSION['me'] : null; } } コード
■Signup.php
namespace MyApp\Controller; class Signup extends \MyApp\Controller{ public function run(){ if ($this->isLoggedIn()) { header('Location: '. SITE_URL); exit(); } if($_SERVER['REQUEST_METHOD'] === 'POST'){ $this->postProcess(); } } protected function postProcess(){ // validate try{ $this->_validate(); } catch(\MyApp\Exception\InvalidEmail $e){ $this->setErrors('email', $e->getMessage()); } catch(\MyApp\Exception\PleaseEnterEmail $e){ $this->setErrors('email', $e->getMessage()); } catch(\MyApp\Exception\InvalidPassword $e){ $this->setErrors('password', $e->getMessage()); } $this->setValues('email', $_POST['email']); if($this->hasError()){//エラーがあったら... return; }else{//エラーがなければ... // create user try{ $userModel = new \MyApp\Model\User(); $userModel->create([ 'email' => $_POST['email'], 'password' => $_POST['password'] ]); }catch(\MyApp\Exception\DuplicateEmail $e){//既にメールが登録されている時のエラー $this->setErrors('email', $e->getMessage()); return; } // redirect to login header('Location: '. SITE_URL . '/login.php'); exit; } } private function _validate(){ //CSRF対策 if(!isset($_POST['token']) || $_POST['token'] !== $_SESSION['token']){ echo "Invalid Token!"; exit; } if($_POST['email'] === ''){ throw new \MyApp\Exception\PleaseEnterEmail(); }elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ throw new \MyApp\Exception\InvalidEmail(); } if(!preg_match('/\A[a-zA-Z0-9]+\z/', $_POST['password'])){ throw new \MyApp\Exception\InvalidPassword(); } } } コード
↓↓↓自分なりに試した方法、結果は値がNULLでした。。。↓↓↓
■Signup.php
$this->setValues('email', $_POST['email']); if($this->hasError()){//エラーがあったら... return; }else{//エラーがなければ... // go to check.php header('Location: '. SITE_URL. "check.php"); exit; } } コード
■check.php
// 予約内容確認画面 require_once(__DIR__ . '/../config/config.php'); $app = new MyApp\Controller\Entry(); var_dump($app->getValues()->email); コード
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/25 05:18
2017/09/27 09:16