├── .gitignore ├── composer.json ├── README.md ├── index.php ├── .htaccess ├── view ├── confirm.php └── input.php ├── app ├── Http │ ├── HttpException.php │ ├── Request.php │ └── route.php ├── controller │ └── ContactController.php └── Validation │ └── Validate.php └── Util.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "autoload":{ 3 | "psr-4":{ 4 | "App\\":"app/" 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pure_php_contact_gorm 2 | 3 | root_dir 4 | 5 | ``` 6 | php -S localhost:8000 7 | ``` 8 | 9 | ``` 10 | composer install 11 | ``` 12 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | dispatch(); 9 | }catch (App\Http\HttpException $e){ 10 | echo "error in index.php {$e->getMessage()}"; 11 | } 12 | -------------------------------------------------------------------------------- /.htaccess: -------------------------------------------------------------------------------- 1 | 2 | RewriteEngine On 3 | RewriteBase / # ここは設置するディレクトリに合わせて変更 4 | RewriteCond %{REQUEST_FILENAME} !-f 5 | RewriteCond %{REQUEST_FILENAME} !-d 6 | RewriteRule ^(.*)$ app/webroot/$1 [QSA] 7 | RewriteCond %{REQUEST_FILENAME} !-f 8 | RewriteCond %{REQUEST_FILENAME} !-d 9 | RewriteRule ^(.*)$ index.php [QSA,L] 10 | -------------------------------------------------------------------------------- /view/confirm.php: -------------------------------------------------------------------------------- 1 | 2 | 3 |

confirm.php

4 |
5 | 6 |
7 |
8 | 9 |
10 |
11 | 12 | 13 |
14 |
15 | 16 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /app/Http/HttpException.php: -------------------------------------------------------------------------------- 1 | code = $code; 15 | $this->message = $message; 16 | header("HTTP/1.0 {$code} {$message}"); 17 | parent::__construct($this->__toString(), $code, null); 18 | } 19 | 20 | // オブジェクトの文字列表現を独自に定義する 21 | public function __toString() { 22 | return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /app/Http/Request.php: -------------------------------------------------------------------------------- 1 | hasQuery($key)?$_GET[$key]:null; 9 | } 10 | 11 | public function getPost($key){ 12 | return $this->hasPost($key)?$_POST[$key]:null; 13 | } 14 | 15 | public function hasQuery($key){ 16 | return isset($_GET[$key])?true:false; 17 | } 18 | public function hasPost($key){ 19 | return isset($_POST[$key])?true:false; 20 | } 21 | 22 | public function getPostAll(){ 23 | return $_POST; 24 | } 25 | 26 | public function getRequestMethod(){ 27 | return $_SERVER["REQUEST_METHOD"]; 28 | } 29 | 30 | public function getReferer(){ 31 | return $_SERVER['HTTP_REFERER']; 32 | } 33 | } -------------------------------------------------------------------------------- /Util.php: -------------------------------------------------------------------------------- 1 | setData($request->getPostAll()); 17 | $rules = [ 18 | 'name' => 'required|max:255', 19 | 'gender' => 'required', 20 | 'comment' => 'max:255|required', 21 | 'email' => 'required|email', 22 | ]; 23 | //set 24 | $validate->setRules($rules); 25 | 26 | if(!$validate->run()){ 27 | $error["error"] = $validate->getMessage(); 28 | view("view/input", $error); 29 | }else{ 30 | view('view/confirm', $request->getPostAll()); 31 | } 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /view/input.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | $v) { 6 | foreach ($v as $key => $value) { 7 | print "{$k} => {$value}
"; 8 | } 9 | } 10 | } 11 | ?> 12 |

input.php

13 |
14 |
15 | 16 | 17 |
18 |
19 | 20 | 21 |
22 |
23 | 24 | 25 |
26 |
27 | 28 | 29 |
30 | 31 |
32 | 33 | -------------------------------------------------------------------------------- /app/Http/route.php: -------------------------------------------------------------------------------- 1 | request = $req; 10 | } 11 | 12 | public function dispatch(){ 13 | 14 | //urlのパラメーターを分ける。 15 | $url = $_SERVER['REQUEST_URI']; 16 | //最初の/をとって/で区切る 最初がcontroller名 次がaction名となる。 17 | 18 | $params = explode("/", trim($url,'/')); 19 | 20 | if (!isset($params[0])){ 21 | throw new HttpException(404,"Class NotFound"); 22 | } 23 | if (!isset($params[1])) { 24 | throw new HttpException(404,"Method NotFound"); 25 | } 26 | 27 | /*paramからcontroller名とAction名をとる*/ 28 | 29 | $_controllerName = $this->ConvertControllerName($params[0]); 30 | //append HttpParameterw 31 | $_controllerNameSpace = "App\\Controller\\{$_controllerName}Controller"; 32 | 33 | if(!class_exists($_controllerNameSpace)){ 34 | throw new HttpException(404,"class Undefined"); 35 | } 36 | 37 | $_controllerInstance = new $_controllerNameSpace(); 38 | 39 | $_lowerRequestMethod = mb_strtolower($this->request->getRequestMethod()); 40 | 41 | $_actionName = "{$_lowerRequestMethod}_{$params[1]}"; 42 | //action判定 43 | if(!method_exists($_controllerInstance, $_actionName)){ 44 | throw new HttpException(404,"Method Undefined"); 45 | } 46 | 47 | //そのアクションメソッドの実行 48 | $_controllerInstance->$_actionName(new Request()); 49 | } 50 | 51 | private function ConvertControllerName($name){ 52 | //全て小文字にする 53 | $_lowerControllerName = mb_strtolower($name); 54 | //先頭文字を大文字に 55 | $_ControllerName = ucfirst($_lowerControllerName); 56 | return $_ControllerName; 57 | } 58 | } -------------------------------------------------------------------------------- /app/Validation/Validate.php: -------------------------------------------------------------------------------- 1 | data = $data; 14 | } 15 | 16 | public function setRules($ruleData){ 17 | $this->ruleData = $ruleData; 18 | } 19 | 20 | public function run(){ 21 | /* $ruleData = ["name" => "required|max:255"] 22 | * ruleの|の切り出し 23 | */ 24 | 25 | foreach ($this->ruleData as $adaptDataKey => $ruleText){ 26 | // |で複数区切られている場合は切り分ける 27 | if(preg_match("/|/", $ruleText)){ 28 | $ruleArray = explode("|", $ruleText); 29 | 30 | foreach ($ruleArray as $rule){ 31 | //切り分け後実行 32 | $this->method_run($rule, $adaptDataKey); 33 | } 34 | }else{ 35 | //切り分けないならそのまま実行 36 | $this->method_run($ruleText, $adaptDataKey); 37 | } 38 | } 39 | 40 | return $this->is_success; 41 | } 42 | 43 | /* 44 | * $rule = 呼び出すruleMethod名 45 | * $key = validationするdataのkey 46 | * */ 47 | private function method_run($rule, $key){ 48 | //ruleに範囲があるならそれを切り出す 49 | if(preg_match("/:/", $rule)){ 50 | $params = explode(":", $rule); 51 | $rule = $params[0]; 52 | $this->num = $params[1]; 53 | } 54 | 55 | if(method_exists(__CLASS__, $rule)){ 56 | //dataがあるか 57 | if(isset($this->data[$key])){ 58 | //rule名のmethodを呼び出す 59 | if(!$this->$rule($key, $this->num)){ 60 | //失敗なら 61 | $this->setMessage($key, $rule, "Need {$rule} type"); 62 | } 63 | }else{ 64 | //Dataがない 65 | $this->setMessage($key, $rule, "Need Set {$rule}"); 66 | } 67 | }else{ 68 | //method false 69 | $this->setMessage($key, $rule, "{$rule} is not define"); 70 | } 71 | } 72 | 73 | private function required($key){ 74 | if(is_null($this->data[$key])){ 75 | return false; 76 | } 77 | //空白を取り除いて空白判定 78 | if((preg_replace("/( | )/", "", $this->data[$key]) == "")){ 79 | return false; 80 | } 81 | return true; 82 | 83 | } 84 | 85 | private function numeric($key){ 86 | return is_numeric($this->data[$key]); 87 | } 88 | 89 | private function email($key){ 90 | return preg_match("/^([a-zA-Z0-9\._-])*@([a-zA-Z0-9\._-]+)/", $this->data[$key]); 91 | } 92 | 93 | private function bool($key){ 94 | return is_bool($this->data[$key]); 95 | } 96 | 97 | private function max($key, $num=1){ 98 | if(strlen($this->data[$key]) <= $num){ 99 | return true; 100 | }else{ 101 | return false; 102 | } 103 | } 104 | 105 | private function min($key, $num=1){ 106 | if(strlen($this->data[$key]) >= $num){ 107 | return true; 108 | }else{ 109 | return false; 110 | } 111 | } 112 | 113 | public function getMessage(){ 114 | return $this->message; 115 | } 116 | 117 | private function setMessage($key, $value, $message){ 118 | $this->message[$key][$value] = $message; 119 | $this->is_success = false; 120 | } 121 | } --------------------------------------------------------------------------------