前提・実現したいこと
書籍パーフェクトPHPのミニブログの写経で
メンバの呼び出しに関するエラーという認識ですが解決できていません。
アドバイスを頂ければ幸いです。
発生している問題・エラーメッセージ
Fatal error: Uncaught Error: Call to a member function getBaseUrl() on null in C:\xampp\htdocs\practice\mini-blog\core\Controller.php:47 Stack trace: #0 C:\xampp\htdocs\practice\mini-blog\controllers\AccountController.php(93): Controller->render(Array) #1 C:\xampp\htdocs\practice\mini-blog\core\Controller.php(38): AccountController->signinAction(Array) #2 C:\xampp\htdocs\practice\mini-blog\core\Application.php(122): Controller->run('signin', Array) #3 C:\xampp\htdocs\practice\mini-blog\core\Application.php(107): Application->runAction('account', 'signin') #4 C:\xampp\htdocs\practice\mini-blog\web\index_dev.php(7): Application->run() #5 {main} thrown in C:\xampp\htdocs\practice\mini-blog\core\Controller.php on line 47
該当のソースコード
Controller.php
php
1 2<?php 3 4abstract class Controller 5{ 6 protected $controller_name; 7 protected $action_name; 8 protected $application; 9 protected $request; 10 protected $response; 11 protected $session; 12 protected $db_manager; 13 protected $auth_actions = []; 14 15 public function __construct($application) 16 { 17 $this->controller_name = strtolower(substr(get_class($this), 0,-10)); 18 19 $this->application = $application; 20 $this->requeest = $application->getRequest(); 21 $this->response = $application->getResponse(); 22 $this->session = $application->getSession(); 23 $this->db_manager = $application->getDbManager(); 24 } 25 26 public function run($action, $params = []) 27 { 28 $this->action_name = $action; 29 30 $action_method = $action . 'Action'; 31 if(!method_exists($this, $action_method)){ 32 $this->forward404(); 33 } 34 35 if ($this->needsAuthentication($action) && !$this->session->isAuthenticated()){ 36 throw new UnauthorizedActionException(); 37 } 38 39 $content = $this->$action_method($params); 40 41 return $content; 42 } 43 44 protected function render($variables = [], $template = null, $layout = 'layout') 45 { 46 $defaults = [ 47 'request' => $this->request, 48 'base_url' => $this->request->getBaseUrl(), 49 'session' => $this->session, 50 ]; 51 52 $view = new View($this->application->getViewDir(), $defaults); 53 54 if (is_null($template)){ 55 $template = $this->action_name; 56 } 57 58 $path = $this->controller_name . '/' . $template; 59 60 return $view->render($path, $variables, $layout); 61 } 62 63 protected function forward404() 64 { 65 throw new HttpNotFoundException('Forwarded 404 page from ' 66 . $this->controller_name . '/' . $this->action_name); 67 } 68 69 protected function redirect($url) 70 { 71 If (!preg_match('#https?://#', $url)){ 72 $protocol = $this->request->isSsl() ? 'https://' : 'http://'; 73 $host = $this->request->getHost(); 74 $base_url = $this->request->getBaseUrl(); 75 76 $url = $protocol . $host . $base_url . $url; 77 } 78 79 $this->response->setStatusCode(302, 'Found'); 80 $this->response->setHttpHeader('Location', $url); 81 } 82 83 protected function generateCsrfToken($form_name) 84 { 85 $key = 'csrf_tokens/' . $form_name; 86 $tokens = $this->session->get($key, []); 87 if (count($tokens) >= 10) { 88 array_shift($tokens); 89 } 90 91 $token = sha1($form_name . session_id() . microtime()); 92 $tokens[] = $token; 93 94 $this->session->set($key, $tokens); 95 96 return $token; 97 } 98 99 protected function checkCsrfToken($form_name, $token) 100 { 101 $key = 'csrf_tokens/' . $form_name; 102 $tokens = $this->session->get($key, []); 103 104 if (false !== ($pos = array_search($token, $tokens, true))){ 105 unset($tokens[$pos]); 106 $this->session->set($key, $tokens); 107 108 return true; 109 } 110 111 return false; 112 } 113 114 protected function needsAuthentication($action) 115 { 116 if ($this->auth_actions === true 117 || (is_array($this->auth_actions) && in_array($action, $this->auth_actions)) 118 ){ 119 return true; 120 } 121 122 return false; 123 } 124 125} 126
Request.php
php
1<?php 2 3class Request 4{ 5 public function isPost() 6 { 7 if($_SERVER['REQUEST_METHOD'] === 'POST'){ 8 return true; 9 } 10 11 return false; 12 } 13 14 public function getGet($name, $default = null) 15 { 16 if(isset($_GET[$name])){ 17 return $_GET[$name]; 18 } 19 20 return $default; 21 } 22 23 public function getPost($name, $default = null) 24 { 25 if(isset($_POST[$name])){ 26 return $_POST[$name]; 27 } 28 29 return $default; 30 } 31 32 public function getHost() 33 { 34 if(!empty($_SERVER['HTTP_HOST'])){ 35 return $_SERVER['HTTP_HOST']; 36 } 37 38 return $_SERVER['SEVER_NAME']; 39 } 40 41 public function isSsl() 42 { 43 if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'){ 44 return true; 45 } 46 return false; 47 } 48 49 public function getRequestUri() 50 { 51 return $_SERVER['REQUEST_URI']; 52 } 53 54 public function getBaseUrl() 55 { 56 $script_name = $_SERVER['SCRIPT_NAME']; 57 58 $request_uri = $this->getRequestUri(); 59 60 if(0 === strpos($request_uri, $script_name)){ 61 return $script_name; 62 } else if (0 === strpos ($request_uri, dirname ($script_name))){ 63 return rtrim(dirname($script_name), '/'); 64 } 65 66 return ''; 67 } 68 69 public function getPathInfo() 70 { 71 $base_url = $this->getBaseUrl(); 72 $request_uri = $this->getRequestUri(); 73 74 if (false !== ($pos = strpos($request_uri, '?'))){ 75 $request_uri = substr($request_uri, 0, $pos); 76 } 77 78 $path_info = (string)substr($request_uri, strlen($base_url)); 79 80 return $path_info; 81 } 82} 83
補足情報(FW/ツールのバージョンなど)
- PHP ver.7
- Windows
- XAMMP環境
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/09 03:15
退会済みユーザー
2018/10/09 05:01
2018/10/18 12:53