我无法获得目录的正确路径名,任何人都可以帮我告诉我如何找到路径名吗?

时间:2017-09-21 08:57:38

标签: php model-view-controller

enter image description here

我是mvc的新手,我正在构建我的mvc,并且在构建它时遇到了一些困难。我收到了这个错误:

  

致命错误:未捕获错误:找不到类'userscontroller'   C:\ xampp \ htdocs \ mvc1 \ app \ core \ app.php:20堆栈跟踪:#0   C:\ xampp \ htdocs \ mvc1 \ public \ index.php(4):app-> __ construct()#1 {main}   在第20行的C:\ xampp \ htdocs \ mvc1 \ app \ core \ app.php中抛出

如果您需要我的目录中包含mvc的任何其他代码,您可以免费询问:)

请帮我解决我在以下代码中犯的错误:

app.php Click here to see the image containing my directory files

    <?php
    class app{


        protected $controller ='userscontroller';
        protected $method ='users';
        protected $params =[];


        function __construct()
        {

        $url =$this->url();

             if(file_exists('../app/controllers/'.$url[0].'.php')){
                $this->controller = $url[0];
                unset($url[0]);
                }
            require_once '../app/controllers/'.$this->controller .'.php';
            $this->controller = new $this->controller;


            if(isset($url[1])){
            if (method_exists($this->controller,$url[1])){
                $this->method = $url[1];
                unset($url[1]);
                }
      }
       $this->params = $url ? array_values($url) :[];
       call_user_func_array([$this->controller,$this->method],$this->params);

       }

      function url(){
      if(isset($_GET['url'])){
  return  explode('/',filter_var(rtrim($_GET['url','     /'),FILTER_SANITIZE_URL));   


  }

  }
  }
      ?>





    [1]: https://i.stack.imgur.com/TEPhW.png

1 个答案:

答案 0 :(得分:1)

使用自动加载器代替此灾难。

有关基本示例,您可以尝试采用此方法:http://www.php-fig.org/psr/psr-4/

但是如果你很懒,那么更好的方法就是使用Composer附带的自动加载器(你应该已经使用过)。有关使用该自动加载器的确切详细信息,请参见此处:https://getcomposer.org/doc/01-basic-usage.md#autoloading

你也应该阅读this post,因为你当前的路由方法有点......嗯...不好。

至于你的路径问题,如果你开始使用
,它可能会消失 绝对路径:include __DIR__ . '/../something/file.php';

P.S。恕我直言,使用include_oncerequire_once可视为代码气味。