Skip to content

Latest commit

 

History

History
71 lines (50 loc) · 1.68 KB

File metadata and controls

71 lines (50 loc) · 1.68 KB

Guideline for using PHP MVC REST API

What is REST API?

If you have been used Facebook, Twitter or any other application in your phone, these applications need to connect to internet for getting and sending data from and to their websites.

Add a new route

For creating a new route you should open Route.php file from Router directory.

There is already exist some examples in the file which you can use them as you need.

<?php

$router->get('/home', 'home@index');

$router->post('/home', 'home@post');

$router->get('/', function() {
    echo 'Welcome ';
});

For getting parameters follow bellow example:

<?php

$router->get('/:name', function($param) {
    echo 'Welcome ' . $param['name'];
});

For example, when I use this url "yourdomin.com/afgprogrammer" I will get following output.

wellcome afgprogrammer

It's just a Piece of cake :)

If you want to send the POST requests follow bellow example:

$router->post('/:name', function($param) {
    echo 'Welcome ' . $param['name'];
});

Database Connection

Consider that for useing database you should edit config.php file before start useing database.

For getting a database connection, you can use bellow sample in Model directory:

<?php

use MVC\Model;

class ModelsHome extends Model {

    public function getAllUser() {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user");
        
        /*
          $query->row : return 1 row
          $qurty->rows : return all rows
          $qurty->num_rows : return rows count
        */
        return $qurty->rows;
    }
}