Using marvel API with MVC in PHP

Using marvel API with MVC in PHP

starting with mysql databases and connection

A few days ago I received a call from a recruitment process, where they asked for the use of the api de marvel to do something simple, a script that would keep the names and images of the super heroes in a database, I decided to make it more personal as a project and I dedicated myself in a few hours to create not a script but a scalable PHP Model View Controller. In this first post I will show you the first point of MVC.

Our first step is to create the file where the controller, db, models and views folder will be. in the main file we create index.php

No alt text provided for this image

It is time to work some code, in index.php we are going to place two elements that we need to start the mvc, which would be the driver files that will take us to the model and the database connection

<?php

require_once("db/conexion.php");
require_once("controllers/databaseController.php")

?>


we create connection.php in db and databaseController.php in controllers...

conexion.php
<?php


class Conexion extends mysqli{
       public static function conecting(){
        $server = 'localhost';
        $user = 'root';
        $pw = '';
        $bd = 'marvel';
        $conectingbd = mysqli_connect(
            $server, 
            $user, 
            $pw,
            $bd);
        return $conectingbd;
    }
    public static function db(){
        $server = 'localhost';
        $user = 'root';
        $pw = '';
        $bd = 'marvel';
        $conexion = mysqli_connect($server, $user, $pw);
        if (!$conexion) {
            die('Error de Conexión (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
        
        mysqli_select_db($conexion, $bd);
            try {
                $ndb = new PDO("mysql:host=$server", $user, $pw);
        
                $ndb->exec("CREATE DATABASE `$bd`;") 
                or die(print_r($ndb->errorInfo(), true));
            } catch (PDOException $e) {
                die("DB ERROR: ". $e->getMessage());
            }
            $mybd = new mysqli($server,$user,$pw,$bd);
                $table1 = "CREATE TABLE  `heromarvel` (
                    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                    heroname varchar(250) ,
                    imgpath varchar(250) ,
                    imgext varchar(250) );";
                $table2 = "CREATE TABLE  `keys` (
                    `id` INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                    `privatekey` varchar(250) ,
                    `publickey` varchar(250) ,
                    `hashi` varchar(250) );";
            if(mysqli_query($mybd, $table1 ) === true && mysqli_query($mybd, $table2 ) === true ){
                printf("we did it");
            }
            
            return $mybd;
            
           
           
    }
}


?>

The explanation of the previous code is, we make a class that extends from mysqli, where we create two functions a connecting call, which will connect to the bd if it exists, and the other one called db, which will create a database if it does not exist with two tables one for the keys (then I explain that I speak here, if you do not know) and another heromarvel where we will keep the heroes. In each table we assign an id that increases itself, and the values of the hero's name, image, extension and for the keys, the key publishes the private one and the hash that I call it hashi to avoid possible conflicts when naming it then return an array with the data base created.

databaseController.php
<?php
    require_once("Models/database.php");
        $db = new Databasemodel();
        $data=$db->database();


    require_once("Views/home.php")



?>

The explanation ... Here two things we need from the file that is in the model called database, with this file we will return a new object called databasemodel which will return a database value and return the home view. we create the database.php file in models

database.php
<?php
class Databasemodel {
    private $conexion;
    private $keys;
    public function database(){
        $server = 'localhost';
        $user = 'root';
        $pw = '';
        $bd = 'marvel';
        $conexion = mysqli_connect($server, $user, $pw);
        
        if(mysqli_select_db($conexion, $bd)){
        echo "databse exists";
        require_once("Controllers\apiController.php");
        }else{
        echo "Databse does not exists";
        $this->conexion=Conexion::db();
        $consult= $this->conexion->query("SELECT * FROM keys;");
        
        require_once("Controllers\apiController.php");

        }      
    }


}



?>

This model is the first one that will connect us to the database, if it exists, the paicontroller will return us if the database does not exist, it will be created and placed in the keys table to save the user's keys. let's do apiController.php in Controllers

apiController.php
<?php
    
    require_once("views/home.php")



?>

We only return the view home...

home.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="views/css/style.css">
    <title>home</title>
</head>
<body>
            <div class="grid-container-1 form-container">
                <div class="title">
                    <h2>WELCOME to MVC MARVEL API</h2>
                </div>
                    <form method="POST" action="models/marvel.php">
                        <div class="imputname">
                            <input type="text" placeholder="your publickey" name="publickey" >
                        </div>
                        <div class="imputname">
                            <input type="text" placeholder="your privatekey" name="privatekey" >
                        </div>


                        <div class="imputname">
                            <input type="submit" class="blackbtn" value="make you DB for this API"> 
                        </div>


                    </form>
                
            </div>
</body>


</html>

this is a simple html code with some classes to style it in css. We do not use libraries like bootstrap but you can do it, it would be simpler so we do not invent the wheel again, but for teaching purposes it is better to write code as crazy ... and understand it ..

at this moment your view should look like this ..

view of MVC marvel



To view or add a comment, sign in

More articles by jose marin

  • Using marvel API with MVC in PHP part 2

    in the last post we made the connection of the mvc in this we will try to test this connection and touch the api de…

Explore content categories