PHP code example of kodicms / laravel-api
1. Go to this page and download the library: Download kodicms/laravel-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/ */
kodicms / laravel-api example snippets
RouteAPI::post('refresh.key', ['as' => 'api.refresh.key', 'uses' => 'API\KeysController@postRefresh']);
// Что равнозначно
Route::post('api.refresh.key{type?}', ['as' => 'api.refresh.key', 'uses' => 'API\KeysController@postRefresh'])->where('type', '\.[a-z]');
use KodiCMS\API\Http\Controllers\Controller;
class MessageController extends Controller
{
public function sendMessage()
{
$message = $this->getRequiredParameter('message');
...
$this->testParam = '...';
// или
$this->jsonResponse['testParam'] = '...';
$this->setMessage($message);
}
}
// return JsonResponse
{
code: 200,
content: null,
testParam: '...'
type: 'content',
method: 'post',
message: 'message text'
}
/**
* @var int
*/
protected $code = Response::ERROR_UNKNOWN;
public function responseArray()
{
return [
'code' => $this->getCode(),
'type' => Response::TYPE_ERROR,
'message' => $this->getMessage(),
];
}
use KodiCMS\API\Exceptions\Exception;
use KodiCMS\API\Http\Response;
class ValidationException extends Exception
{
/**
* @var int
*/
protected $code = Response::ERROR_VALIDATION;
public function getFailedRules()
{
...
}
public function getErrorMessages()
{
...
}
/**
* @return array
*/
public function responseArray()
{
$data = parent::responseArray();
$data['failed_rules'] = $this->getFailedRules();
$data['errors'] = $this->getErrorMessages();
return $data;
}
}