1. Go to this page and download the library: Download ankitgs/restapi library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace Application\Controller;
use restapi\Controller\ApiController;
/**
* Foo Controller
*/
class FooController extends ApiController
{
/**
* bar method
*
*/
public function barAction()
{
// your action logic
// Set the HTTP status code. By default, it is set to 200
$this->httpStatusCode = 200;
// Set the response
$this->apiResponse['you_response'] = 'your response data';
return $this->createResponse();
}
}
public function login()
{
/**
* process your data and validate it against database table
*/
// generate token if valid user
// $this->tokenPayload you can access user details.
$this->tokenPayload = ['email' => $user->email, 'name' => $user->name];
$this->generateToken();
// $this->token through you can get token.
$this->apiResponse['token'] = $this->token;
$this->apiResponse['message'] = 'Logged in successfully.';
return $this->createResponse();
}
namespace Application\Controller;
use restapi\Controller\ApiController;
/**
* Articles Controller
*
*
*/
class ArticlesController extends ApiController
{
/**
* index method
*
*/
public function indexAction()
{
$articles = $this->entityManager->getRepository(Article::class)
->findBy([], ['id'=>'ASC']);
$this->apiResponse['articles'] = $articles;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.