0

I have the following code in my Core.php file:

<?php 
  /*
   * App Core Class
   * Creates URL and loads core controller
   * URL FORMAT - /controller/method/params
  */

  class Core {
    protected $currentController = 'Pages';
    protected $currentMethod = 'index';
    protected $params = [];

    public function __construct(){
        // print_r($this->getUrl());

        $url = $this->getUrl();

        // Look in controllers for first part of URL
        if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){
          // If exists, set as controller
          $this->currentController = ucwords($url[0]);
          // Unset 0 Index
          unset($url[0]);
        } 

        // Require the controller 
        require_once '../app/controllers/'. $this->currentController . '.php';

        // Instantiate controller class
        // $pages = new Pages;
        $this->currentController = new $this->currentController;

        // Check for second part of URL
        if(isset($url[1])){
            // Check to see if method exists in controller
            if(method_exists($this->currentController, $url[1])){
              $this->currentMethod = $url[1];

            // Unset 1 Index
            unset($url[1]);
            }
        }

        // Get params
        // Array([[0] => 'about', [1] => 1])
        $this->params = $url ? array_values($url) : [];

        // Call a callback with array of params
        call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
    }

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

Upon loading my controller inside /controllers/Pages which contains the following code:

<?php
  class Pages {
    public function __construct(){
       
    }

    public function index(){
      echo "This is index method!";
    }

    public function about($id){
      echo "This is about method and the id passed is " . $id;
    }
  }

It saying something like this:

Warning: Trying to access array offset on value of type null in /Applications/XAMPP/xamppfiles/htdocs/mvc/app/libraries/core.php on line 19

This pertains to this line on my core.php file:

 if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){

I am not sure what does it mean honestly. Can anyone point out what's causing this error even though I have the default index method on my controller?

Thanks in advance.

Kurt Math
  • 13
  • 1
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – Markus Zeller Jan 13 '23 at 13:30
  • It's trying to access the first array key of `$url`, which is generated by `$this->url`. That only returns a value if `$_GET['url']` exists, which would be a query string in your URL, such as `http://localhost/?url=Pages` – aynber Jan 13 '23 at 13:35
  • The variable ``$url`` is null, that's why. Please check your method ``getUrl``, to debug the code use function ``var_dump($variable); exit;`` – tirta keniten Jan 13 '23 at 13:36

1 Answers1

0

$url[0] array variable is not set. that the cause of the error. please add if statement before where you check the file like

if(isset($url[0])){
  if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){
     // If exists, set as controller
     $this->currentController = ucwords($url[0]);
     // Unset 0 Index
     unset($url[0]);
   }
}
atta afridi
  • 111
  • 5