0

enter image description here

I am new to mvc,I am currently building my on mvc and m finding some difficulties in building it.I am getting this error:

Fatal error: Uncaught Error: Class 'userscontroller' not found in C:\xampp\htdocs\mvc1\app\core\app.php:20 Stack trace: #0 C:\xampp\htdocs\mvc1\public\index.php(4): app->__construct() #1 {main} thrown in C:\xampp\htdocs\mvc1\app\core\app.php on line 20

If you need anyother code of mvc included in my directories,you are free ask :)

Please help me to resolve the error I have made in the following code:

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
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
Robin Hood
  • 29
  • 2
  • 11
  • 1
    Don't you think it is still insecure to directly initialize a class depending on the user input? If you someday forget and put a class that contains exploitable code in ../app/controllers, it will call the constructor and do something you don't expect it to do. – SOFe Sep 21 '17 at 09:02
  • Yes I know its insecure but currently I only want to run this code anyhow. – Robin Hood Sep 21 '17 at 09:09
  • Try outputting the path to the file and stop the execution with die() and check if the path is what you expect it to be. – Vindur Sep 21 '17 at 09:22

1 Answers1

1

Instead of this disaster, use an autoloader.

For a basic example you can try adopting this: http://www.php-fig.org/psr/psr-4/

But if you are lazy, a much better approach would be to use the autoloader, that comes with Composer (which you should be already using). The exact details of using that autoloader are explained here: https://getcomposer.org/doc/01-basic-usage.md#autoloading

You also should probably read this post, since you current approach to routing is somewhat ... emm ... not good.

As for your path issue, it would probably go away, if you started using
absolute paths: include __DIR__ . '/../something/file.php';

P.S. IMHO, the use of include_once and require_once can be considered a code smell.

tereško
  • 58,060
  • 25
  • 98
  • 150