1. Go to this page and download the library: Download scorpjio/zf3-rest-api 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/ */
scorpjio / zf3-rest-api example snippets
return [
....
//add this if not available
'RestApi'
];
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
$payload = ['email' => $user->email, 'name' => $user->name];
$this->apiResponse['token'] = $this->generateJwtToken($payload);
$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()
{
// $this->token gives you to token which generated.
// $this->tokenPayload gives you to payload details which you sets at the time of login or generate token.
$payload = $this->tokenPayload;
$articles = $this->entityManager->getRepository(Article::class)
->findBy([], ['id'=>'ASC']);
$this->apiResponse['articles'] = $articles;
return $this->createResponse();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.