2017-04-23 1 views
0

나는 다음과 같은 오류가 계속 주어 나는 확실한 이유에 아니에요 null로 :이 유형의 배열이어야합니다,

잡아낼 치명적인 오류 : 시스템 \ 로더에 전달 된 인수 3 : 행동()해야합니다 유형 어레이의 널 라인 31 /Application.php 호출, 주어진 라인에 정의 /Loader.php 18

Loader 클래스 :

<?php 

namespace System; 

class Loader{ 
    //application object 
    private $app; 
    //controllers container of array 
    private $controllers = []; 
    //models container array 
    private $models = []; 
    //constructor takes in system/application $app 
    public function __construct(Application $app){ 
     $this->app = $app; 
    } 
    //call the given controller with the given method and pass the given arguments to the controller method 
    //takes in string $controller, string $method and array $arguements returns mixed 
    public function action ($controller, $method, array $arguements){ ##line 18 
     $object = $this->controller($controller); 
     return call_user_func([$object, $method], $arguements); 
    } 
    //call the given controller takes in string $controller and returns object 
    public function controller ($controller){ 
     $controller = $this->getControllerName($controller); 
     if(! $this->hasController($controller)){ 
      $this->addController($controller); 
     } 
     return $this->getController($controller); 
    } 
    //determine if the given clas/controller exists in the controller container takes in string $controller and returns boolean 
    private function hasController($controller){ 
     return array_key_exists($controller, $this->controllers); 
    } 
    //create new object for the given controller and store it in the controllers container 
    //takes in string $controller and returns void 
    private function addController($controller){ 
     $object = new $controller($this->app); 
     $this->controllers[$controller] = $object; 
    } 
    //get the controller object takes in string $controller and returns object 
    private function getController($controller){ 
     return $this->controllers[$controller]; 
    } 
    //get the full class name for the given controller takes string $controller returns string 
    private function getControllerName($controller){ 
     $controller .= 'Controller'; 
     $controller = 'App\\Controllers\\' . $controller; 
     return str_replace('/', '\\', $controller); 
    } 

    //call the given Model takes in string $Model and returns object 
    public function model ($model){ 
     $model = $this->getModelName($model); 
     if(! $this->hasModel($model)){ 
      $this->addModel($model); 
     } 
     return $this->getModel($model); 
    } 
    //determine if the given clas/Model exists in the Model container takes in string $controller and returns boolean 
    private function hasModel($model){ 
     return array_key_exists($model, $this->models); 
    } 
    //create new object for the given Model and store it in the controllers container 
    //takes in string $Model and returns void 
    private function addModel($model){ 
     $object = new $model($this->app); 
     $this->models[$model] = $object; 
    } 
    //get the Model object takes in string $Model and returns object 
    private function getModel($model){ 
     return $this->models[$model]; 
    } 
    //get the full class name for the given Model takes string $Model returns string 
    private function getModelName($model){ 
     $model .= 'Model'; 
     $model = 'App\\Models\\' . $model; 
     return str_replace('/', '\\', $model); 
    } 

} 

애플리케이션 클래스 :

<?php 

namespace System; 

class Application { 
    //Container array var 
    private $container = []; 
    //application object 
    private static $instance; 
    //constructor $file param 
    private function __construct(File $file){ 
     $this->share('file', $file); 
     $this->registerClasses(); 
     // static::$instance = $this; 
     $this->loadHelpers(); 

    } 
    //get application instance parameter $file returns system\application 
    public static function getInstance($file = null){ 
     if(is_null(static::$instance)) { 
      static::$instance = new static($file); 
     } 
     return static::$instance; 
    } 
    //run the application returns void 
    public function run(){ 
     $this->session->start(); 
     $this->request->prepareUrl(); 
     $this->file->call('App/index.php'); 
     list($controller, $method, $arguments) = $this->route->getProperRoute(); 
     $output = (string) $this->load->action($controller, $method, $arguments); ##line 31 
     $this->response->setOutput($output); 
     $this->response->send(); 

    } 
    //register classes in sql auto load register 
    private function registerClasses(){ 
     spl_autoload_register([$this, 'load']); 
    } 
    //load class through autoloading takes string $class and returns void 
    public function load($class){ 
     if(strpos($class, 'App') === 0){ 
      $file = $class . '.php'; 
     } else { 
      //get the class from vendor 
      $file = 'vendor/' . $class . '.php'; 
     } 
     if($this->file->exists($file)){ 
      $this->file->call($file); 
     } 
    } 
    //load helpers file returns void 
    private function loadHelpers(){ 
     $this->file->call('vendor/helpers.php'); 
    } 
    // get shared value takes in string $key returns mixed 
    public function get($key){ 
     if(! $this->isSharing($key)){ 
      if($this->isCoreAlias($key)){ 
       $this->share($key, $this->createNewCoreObject($key)); 
      } else{ 
       die('<b>' . $key . '</br> not found in application container'); 
      } 
     } 
     return $this->container[$key]; 
     // return $this->isSharing() ? $this->container[$key] : null; 
    } 
    //determind if the given key is shared through application takes string $key returns boolean 
    public function isSharing($key){ 
     return isset($this->container[$key]); 
    } 
    //share key and value through application 
    public function share($key, $value){ 
     $this->container[$key] = $value; 
    } 
    //determines if the given key is an alias to core class takes string $alias returns boolean 
    public function isCoreAlias($alias){ 
     $coreClasses = $this->coreClasses(); 
     return isset($coreClasses[$alias]); 
    } 
    //create new object for the core class based on the given alias takes in string $alias returns object 
    public function createNewCoreObject($alias){ 
     $coreClasses = $this->coreClasses(); 
     $object = $coreClasses[$alias]; 
     return new $object($this); 
    } 
//get all core classes with its aliase returns array 
private function coreClasses(){ 
    return [ 
      'request'  => 'System\\Http\\Request', 
      'response'  => 'System\\Http\\Response', 
      'session'  => 'System\\Session', 
      'route'   => 'System\\Route', 
      'cookie'  => 'System\\Cookie', 
      'load'   => 'System\\Loader', 
      'html'   => 'System\\Html', 
      'db'   => 'System\\Database', 
      'view'   => 'System\\View\\ViewFactory', 
      'url'   => 'System\\Url', 
    ]; 
} 
//get shared value dynamically takes string $key returns mixed 
public function __get($key){ 
    return $this->get($key); 
} 

누군가가 나를 알아 내도록 도울 수 있다면 어디에서이 null을 얻는 것이 좋을지 모르겠습니다.

편집 : 경로 클래스

<?php 

namespace System; 

class Route { 
    //application object 
    private $app; 
    //routes container 
    private $routes = []; 
    //not found url 
    private $notFound; 
    //constructor 
    public function __construct(Application $app){ 
     $this->app = $app; 
    } 
    //add new route takes string $url string $action and string $requestmethod returns void 
    public function add ($url, $action, $requestMethod = 'GET'){ 
     $route = [ 
      'url'  => $url, 
      'pattern' => $this->generatePattern($url), 
      'action' => $this->getAction($action), 
      'method' => strtoupper($requestMethod), 
      ]; 
     $this->routes[] = $route; 
    } 
    //set not found url take string $url returns void 
    public function notFound($url){ 
     $this->notFound = $url; 
    } 
    //get proper route reutns array 
    public function getProperRoute(){ 
     foreach ($this->routes as $route) { 
      if ($this->isMatching($route['pattern'])){ 
       $arguments = $this->getArgumentsFrom($route['pattern']); 
       list($controller, $method) = explode('@', $route['action']); 
       return [$controller, $method, $arguments]; 
      } 
     } 
    } 
    //determine if the given pattern matches the current requested url takes string $pattern returns boolean 
    private function isMatching($pattern){ 
     return preg_match($pattern, $this->app->request->url()); 
    } 
    // get arguement from the current requested url based on the given pattern takes in string $pattern returns array 
    private function getArgumentsFrom($pattern){ 
     preg_match($pattern, $this->app->request->url(), $matches); 
     array_shift($matches); 
     return $matches; 
    } 
    //generate a regex pattern for the given url takes string url returns string 
    private function generatePattern($url){ 
     $pattern = '#^'; 
     // :text ([a-zA-Z0-9-]+) 
     // :id (\d+) 

     $pattern .= str_replace([':text', ':id'], ['([a-zA-Z0-9-]+)', '(\d+)'], $url); 
     $pattern .= '$#'; 
     return $pattern; 
    } 

    //get proper action takes in string $action returns string 
    private function getAction($action){ 
     $action = str_replace('/', '\\', $action); 
     return strpos($action, '@') !== false ? $action : $action . '@index'; 
    } 
} 
+0

의 반환 당신이 그것을 디버깅 적이 무엇인지 확인? 그것은 어딘가에서오고 있어야합니다. – Carcigenicate

+0

그래서'$ this-> route-> getProperRoute()'를 호출하여'$ arguments'에 대해 무엇을 돌려 주나요? –

답변

-1

호출 getProperRoute()는 2 개 요소의 배열을 반환하고, "$의 가지 인자가"NULL 이유입니다.

당신은 호출 변경할 수 있습니다 :

public function action ($controller, $method, array $arguements = []){ 
    $object = $this->controller($controller); 
    return call_user_func([$object, $method], $arguements); 
} 

을 또는 getProperRoute 방법

+0

나는 그것을 시도했지만 여전히 똑같은 것을 얻었고, 라우트 클래스도 추가 할 것이고, 나는 그것과 아무런 관련이 없다고 볼 수있다. < –

+0

정말 시도해 보셨습니까? 이 링크를 확인하십시오. [link] (http://phpfiddle.org/lite?code=%3C?php\n\nfunction%20test ($ a, array % 20 $ b % 20 = % 20 []) \ \ n \ n % 20print (% 22 값 % 20 : % 20 % 25 %, % 20 % 25 % 22, % 20 $ a, % 20join (% 27 :: % 27, $ b)); \ n} \ n \ \ n \ n? % 3E \ n) ntest (% 27a % 27); \ n 테스트 (% 27a % 27, % 20 [% 27a % 27, % 27b % 27, % 20 % –

관련 문제