GithubHelp home page GithubHelp logo

afgprogrammer / php-mvc-rest-api Goto Github PK

View Code? Open in Web Editor NEW
145.0 8.0 40.0 53 KB

A simple PHP MVC REST API framework with PHP 7.2 With routes and some tools to develop your API.

License: MIT License

PHP 100.00%
php mvc-php rest-api framework php72 php-framework mvc-framework mvc simple-framework framework-php

php-mvc-rest-api's Introduction

Guideline for using PHP MVC REST API

What is REST API?

A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding.

An API is a set of definitions and protocols for building and integrating application software. It’s sometimes referred to as a contract between an information provider and an information user—establishing the content required from the consumer (the call) and the content required by the producer (the response). For example, the API design for a weather service could specify that the user supply a zip code and that the producer reply with a 2-part answer, the first being the high temperature, and the second being the low.

Introduction

Simply, the framework will route requests to the correct controller and model. It will do this by analysing request URI for the controller name and the request type (be it POST, PUT, GET, etc.). It will then do some sanity checks, before initialising a new controller and model object and calling the correct method on the controller.

Documentation

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 below 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.

Welcome afgprogrammer

It's just a Piece of cake :)

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

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

Database Connection

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

For getting a database connection, you can use below 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
          $query->rows : return all rows
          $query->num_rows : return rows count
        */
        return $query->rows;
    }
}

php-mvc-rest-api's People

Contributors

afgprogrammer avatar angelokezimana avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-mvc-rest-api's Issues

Multiple matched routes in $this-matchRouter

When I had multiple routes like auth/login, auth/:custom/ , /:parent/:alias/ and my current route is auth/login/ i get all three routes in $this->matchRouter, it is possible to fix that?
image
image
image
where /auth/alias normally are /:parent/:alias/
Also I get all params from the routes:
image

Fatal error: Uncaught Exception: Class { MVC\Exception } Not Found!

Hello
For me, this set of code (library) works fine in the local environment of the system
But when I upload and set it on my own subdomains, and I make a request, the following error message is received:

Fatal error: Uncaught Exception: Class { MVC\Exception } Not Found! in /home/myshop/api/System/Startup.php:12 Stack trace: #0 [internal function]: autoload('MVC\\Exception') #1 /home/myshop/api/System/MVC/Controller.php(54): spl_autoload_call('MVC\\Exception') #2 /home/myshop/api/Application/Controllers/Buyer.php(192): MVC\Controller->model('buyer') #3 [internal function]: ControllersBuyer->find(Array) #4 /home/myshop/api/System/Router/Router.php(240): call_user_func(Array, Array) #5 /home/myshop/api/System/Router/Router.php(209): Router\Router->runController(Object(ControllersBuyer), Array) #6 /home/myshop/api/index.php(31): Router\Router->run() #7 {main} thrown in /home/myshop/api/System/Startup.php on line 12

I checked, the received data and GET and POST parameters are received correctly and the error occurs when the model class is called
I think it is from the addressing in the config.php file or the autoload() function

PHp-mvc-rest-api

I liked your project a lot.. But I got some problem while accessing an URL which contains parameters with spaces like /fetch-name/syam Sundararao .
If I gave this I got route not found...
Please suggest me.. I studied your dispatch code. But unable to fix it...

Showing "not allowed" in Postman every time i use get method

Hi, your work is really admirable. I am facing an issue. The response is working fine on the localhost but on the server, it returns the message "not allowed" when I use the GET method. On POST it works fine. I am sorry I don't want to put this as issue. But i tried multiple things and it isn't resolved the issue

Cors error in ajax call

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at "url" (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

but in postman or android (java) not problem

$.ajax({
url: 'https://example.com',
type: 'POST',
data: {
id: "2"
},
success: function (result) {
console.log(result)
}, error:function() {
console.log("error")
}
})

why?

Adding query parameters to URL route?

Is it possible to pass additional query parameters on a route, and if so, how can we capture those parameters in tandem with the route parameters?

For example, if you have the route:

// search books
$router->get('/books/title/:title', 'Books@searchBooksByTitle');

Could append the URL to include some additional parameters like:

https://localhost/books/title/Harry?sort=publish_date

I am running into issues when I attempt that it says the requested route is not found, and I believe that is because its explicitly looking for a route to match with the query parameters on it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.