MVC (Model View Controller) Tutorial Errors

Home Forums Bugs MVC (Model View Controller) Tutorial Errors

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1385
    Erjon Rrapi
    Participant
    • Warning: Undefined array key “url” in C:\xampp\htdocs\Practice\MVC\public\index.php on line 8

    Occurs when we try to get to somewhere with the url but there is nothing in the url

    Solution : We create a variable that gets the url and if it doesn’t exists it redirects to the homepage .

    Like this ->$URL= $_GET[‘url’] ?? ‘home’;

    • Fatal error: Uncaught Error: Call to undefined function loadController() in C:\xampp\htdocs\Practice\MVC\app\core\App.php:26 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\app\core\init.php(7): require() #1 C:\xampp\htdocs\Practice\MVC\public\index.php(3): require(‘C:\\xampp\\htdocs…’) #2 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\core\App.php on line 26

    Occurs when we try to load the function but in our case the function is part of the class and it’s private.
    Solution : We make the method public instead of private.

     

    • Fatal error: Uncaught Error: Call to undefined function splitURL() in C:\xampp\htdocs\Practice\MVC\app\core\App.php:14 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\public\index.php(6): App->loadController() #1 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\core\App.php on line 14

    We wanted to call a function inside the class but instead we called it as it was outside the class.

    Solution : We use $this-> in order to call te function inside the class.

     

    • Fatal error: Uncaught Error: Unknown named parameter $a in C:\xampp\htdocs\Practice\MVC\app\controllers\Home.php:14 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\app\core\App.php(18): require() #1 C:\xampp\htdocs\Practice\MVC\public\index.php(6): App->loadController() #2 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\controllers\Home.php on line 14

    We wanted to pass the data from one function to the array but we didn’t declare the actual parameters on the function itself

    Solution : We should not forget to declare the actual parameters in the function. Our case was :

    Function:

    public function index($a = ‘ ‘, $b = ‘ ‘)
    {
    echo “This is the home controller”;
    }

    Trying to call the function with its parameters:

    call_user_func_array([$home, ‘index’], [‘a’ => ‘ a something’, ‘b’ => ‘b something’]);

     

    • Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, class Home does not have a method “index3” in C:\xampp\htdocs\Practice\MVC\app\controllers\Home.php:15 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\app\core\App.php(18): require() #1 C:\xampp\htdocs\Practice\MVC\public\index.php(6): App->loadController() #2 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\controllers\Home.php on line 15

    Occurs when we try to call a function but the name of the function that we are trying to access is wrong, so we get an invalid callback. In our case we called the function with the name “index3” instead of “index”.

    public function index($a = ”, $b = ”)
    {
    echo “This is the home controller”;
    }

    call_user_func_array([$home, ‘index3’], [‘a’ => ‘ a something’, ‘b’ => ‘b something’]);

     

    Solution: We should be careful with the names and call the functions correctly.

     

    • Warning: Undefined variable $method in C:\xampp\htdocs\Practice\MVC\app\core\App.php on line 34
    • Warning: Undefined property: App::$ in C:\xampp\htdocs\Practice\MVC\app\core\App.php on line 34
    • Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, second array member is not a valid method in C:\xampp\htdocs\Practice\MVC\app\core\App.php:34 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\public\index.php(6): App->loadController() #1 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\core\App.php on line 34

    We got this series of errors when we tried to call the controller. It tells us that the “method” variable is not defined and the callback function is not valid. If we check carefully the callback function we see that we’ve made a typo when trying to call the method . Instead of $this->method we’ve written $this->$method.

    call_user_func_array([$controller, $this->$method], [‘a’ => ‘ a something’, ‘b’ => ‘b something’]);

     

    Solution: We check carefully that we pass the correct method name in order for the callback to work.

    call_user_func_array([$controller, $this->method], [‘a’ => ‘ a something’, ‘b’ => ‘b something’]);

     

    • Fatal error: Uncaught Error: Class “_404” not found in C:\xampp\htdocs\Practice\MVC\app\core\App.php:34 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\public\index.php(6): App->loadController() #1 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\core\App.php on line 34

    We are trying to redirect to the 404 page but it cannot find the class.

    Solution : Make sure that the connection with the page is correct or that the page has any content to display .

     

    • Fatal error: Uncaught Error: Call to undefined function PDO() in C:\xampp\htdocs\Practice\MVC\app\core\Database.php:4 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\app\core\init.php(4): require() #1 C:\xampp\htdocs\Practice\MVC\public\index.php(3): require(‘C:\\xampp\\htdocs…’) #2 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\core\Database.php on line 4

    Occurs when we call a function but it is not defined. In our case we were trying to call for a class but accidentally called for  a function.

    $con =  PDO($string, ‘root’, ”);

    Solution: Generally make sure that the function name is correct or the function is defined but in our case we forgot to add  ‘new’ keyword before calling the class.

    $con =  new PDO($string, ‘root’, ”);

     

    • Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1049] Unknown database ‘my_db’ in C:\xampp\htdocs\Practice\MVC\app\core\Database.php:4 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\app\core\Database.php(4): PDO->__construct(‘mysql:hostname=…’, ‘root’, ”) #1 C:\xampp\htdocs\Practice\MVC\app\core\init.php(4): require(‘C:\\xampp\\htdocs…’) #2 C:\xampp\htdocs\Practice\MVC\public\index.php(3): require(‘C:\\xampp\\htdocs…’) #3 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\core\Database.php on line 4

    We tried to connect to the database my_db but we get an error that the database is unknown.

    Solution:Make sure that you have created a database or it exists and if it does check the spelling for its name.

    • Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in C:\xampp\htdocs\Practice\MVC\app\core\Database.php:20 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\app\core\Database.php(20): PDOStatement->execute(Array) #1 C:\xampp\htdocs\Practice\MVC\app\core\Model.php(7): Database->query(‘select * from u…’) #2 C:\xampp\htdocs\Practice\MVC\app\controllers\Home.php(8): Model->test() #3 C:\xampp\htdocs\Practice\MVC\app\core\App.php(43): Home->index() #4 C:\xampp\htdocs\Practice\MVC\public\index.php(6): App->loadController() #5 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\core\Database.php on line 20

    Here we are trying to connect to the database but it says that the connection can’t be made because the database is not being selected. From this we can tell that if the connection and the logic is right then there is something to do with the syntax, the way we are trying to connect to the database. In our case we forgot to add a semicolon “;” when writing “dbname”. When we try to connect to the database it will not know which is the actual database so it will throw this error.

    $string = “mysql:hostname=” . DBHOST . “dbname=” . DBNAME;

    Solution : We add the semicolon and make sure that everything according to typos and pronounciation is correct. This should solve the problem.

    $string = “mysql:hostname=” . DBHOST . “;dbname=” . DBNAME;

     

    • Fatal error: Class Model cannot extend from trait Database in C:\xampp\htdocs\Practice\MVC\app\core\Model.php on line 10

    The error is suggesting that you cannot extend from trait since we are trying to access a trait like it was a class using the “extends” keyword.

    Solution: We make sure to call traits and classes using the correct syntax, “use traitname” for traits and “extends classname” for classes.

     

    • Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\Practice\MVC\app\core\Database.php:20 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\app\core\Database.php(20): PDOStatement->execute(Array) #1 C:\xampp\htdocs\Practice\MVC\app\core\Model.php(70): Model->query(‘insert into use…’, Array) #2 C:\xampp\htdocs\Practice\MVC\app\controllers\Home.php(11): Model->insert(Array) #3 C:\xampp\htdocs\Practice\MVC\app\core\App.php(43): Home->index() #4 C:\xampp\htdocs\Practice\MVC\public\index.php(6): App->loadController() #5 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\core\Database.php on line 20

    We are trying to insert some values to the array but it says that the number of parameters does not match the number of tokens.

    Solution:After debugging what values does the query and the values hold we see that we have made a mistake when passing the values.

    Query: insert into users(name,age,date) values (name,:age,:date)

    As we can see we the “name” value does not have : at the begining so id does not pass as a value.

    We correct the code and now it should work as intended.

    Query: insert into users(name,age,date) values (:name,:age,:date)

     

    • Fatal error: Uncaught Error: Class “User” not found in C:\xampp\htdocs\Practice\MVC\app\controllers\Home.php:8 Stack trace: #0 C:\xampp\htdocs\Practice\MVC\app\core\App.php(43): Home->index() #1 C:\xampp\htdocs\Practice\MVC\public\index.php(6): App->loadController() #2 {main} thrown in C:\xampp\htdocs\Practice\MVC\app\controllers\Home.php on line 8

    From the error we understand that we are trying to call a class but it is undefined.

    Solution: We use the spl_autoload_register() where we find the file and we use require to get it.

     

     

     

     

     

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.