PHP Classes

Learn How to Implement an API Using JSON Files to Store Information From a PHP Web Service Example Application PackageWeb Service PHP JSON: API example that stores information in JSON files

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-11-22 (2 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 18 This week: 18All time: 11,353 This week: 5Up
Version License PHP version Categories
webservicephpjson 1.0.0The PHP License5PHP 5, Web services, Design Patterns, A...
Description 

Author

This package implements an API example that stores information in JSON files.

It provides a script that defines mappings between request URL patterns and controller classes that implement the actions associated with each supported API call.

The controller classes use JSON files to store and retrieve information to implement each API call action.

Picture of António Lira Fernandes
Name: António Lira Fernandes <contact>
Classes: 13 packages by
Country: Portugal Portugal
Innovation award
Innovation award
Nominee: 3x

Winner: 2x

 

Example

<?php
use src\Route as Route;
use
classes\authentication\Authentication;

Route::get('/', function(){require _CAMINHO_TEMPLATE. "index.html";});
Route::get('/admin/json', function(){require _CAMINHO_TEMPLATE. "tabelaCarros.html";});

Route::get('/timeline', function(){require _CAMINHO_TEMPLATE. "timeline.html";});

Route::get(['set' => '/base/index', 'as' => 'base.index'], 'Controller@index');
Route::get(['set' => '/base/show/{id}', 'as' => 'base.show'], 'Controller@show');

//carros
Route::get(['set' => '/api/carros', 'as' => 'carros.getAll'], 'ControllerCarros@getAll');
Route::get(['set' => '/api/carros/{id}', 'as' => 'carros.getById'], 'ControllerCarros@getById');
Route::post(['set' => '/api/carros/create', 'as' => 'carros.create'], 'ControllerCarros@create');
Route::put(['set' => '/api/carros/update', 'as' => 'carros.update'], 'ControllerCarros@update');
Route::delete(['set' => '/api/carros/delete/{id}', 'as' => 'carros.index'], 'ControllerCarros@delete');

//Zona sem autenticação
//Artigos
Route::get(['set' => '/artigos/numeros', 'as' => 'artigos.contarArtigos'], 'ControllerArtigos@contarArtigos');
Route::get(['set' => '/artigo/{id}/ver', 'as' => 'artigos.ArtigoVer'], 'ControllerArtigos@ArtigoVer'); //web service
Route::get('/artigo/ver/{id}', function(){ require _CAMINHO_TEMPLATE1. "artigo.php";}); //ver artigo
Route::get('/artigo/ver/', function(){ require _CAMINHO_TEMPLATE1. "artigo.php";}); //ver artigo

//Users
Route::get(['set' => '/users/contar', 'as' => 'users.contarUsers'], 'ControllerUser@contarUsers');
Route::get(['set' => '/users/lista', 'as' => 'users.listOfUsers'], 'ControllerUser@listOfUsers');

//Autenticação
$aut=new Authentication();
if (
$aut->isLoged()){
 
//Zona com autenticação

 
Route::get('/admin/json', function(){require _CAMINHO_TEMPLATE. "tabelaCarros.html";});
 
 
 
//Users
 
Route::get('/users', function(){ require _CAMINHO_ADMIN. "utilizadoresGere.php";}); //mostra todos os users
 
Route::post('/users', function(){ require _CAMINHO_ADMIN. "utilizadoresGere.php";});
 
 
//Artigos
 
Route::get('/artigos', function(){ require _CAMINHO_ADMIN. "artigosGerir.php";}); //mostra os últimos artigos
 
Route::post('/artigos', function(){ require _CAMINHO_ADMIN. "artigosGerir.php";});
 
Route::post(['set' => '/artigo/add', 'as' => 'artigos.addArtigo'], 'ControllerArtigos@addArtigo');
 
Route::get(['set' => '/artigo/add', 'as' => 'artigos.addArtigo'], 'ControllerArtigos@addArtigo');
}else{
 
//echo "Não tem acesso";
  //header('Location: https://www.esmonserrate.org/public/semAcesso');
  //exit;
 
Route::get('/{any}', function(){ require _CAMINHO_ERROS. "erro401.php";});
 
Route::get('/{any}/{any}', function(){ require _CAMINHO_ERROS. "erro401.php";});
 
Route::get('/{any}/{any}/{any}', function(){ require _CAMINHO_ERROS. "erro401.php";});
}

Route::get('/{any}', function(){ require _CAMINHO_ERROS. "erro404.php";});
Route::get('/{any}/{any}', function(){ require _CAMINHO_ERROS. "erro404.php";});
Route::get('/{any}/{any}/{any}', function(){ require _CAMINHO_ERROS. "erro404.php";});

?>


Details

routes

The goal of this package is to implement a routing mechanism to present beautiful URLs. With this mechanism, all requests are made in the public folder and then redirected to a specific file according to the route (URL) written.

version

Version 1.0 2023/03/06

install

  1. Copy all files to the website root.
  2. Update .htaccess for the actual configuration. Change line 3 to set for the url to public folder.

edit the routes file to match a route to a file. Each route or set of routes should correspond to a website service. In order to edit the routes.php file to make the correspondence you need to open the file and add the desired route and the corresponding service. For example, if you want to match the route "/my-route" to the "my-service" service, you can add the following line to the routes.php file:

Route::get('/my-route', 'MyService@index'); This will make it so that when a request is sent to the "/my-route" URL, the "MyService" service is invoked. You can also use route parameters and wildcards to make more complex matches. For more information on the routing system in PHP, please refer to the official documentation.


  Files folder image Files (235)  
File Role Description
Files folder image.history (3 files, 4 directories)
Files folder imageapp (2 files)
Files folder imageclasses (1 file, 1 directory)
Files folder imageimage (5 files)
Files folder imagejson (4 files)
Files folder imagepublic (2 files)
Files folder imageroutes (1 file)
Files folder imagesrc (7 files, 1 directory)
Files folder imagetemplates (2 directories)
Accessible without login Plain text file .htaccess Data Auxiliary data
Accessible without login Plain text file autoload.php Aux. Configuration script
Accessible without login Plain text file bootstrap.php Example Example script
Accessible without login Plain text file config.php Aux. Configuration script
Accessible without login Plain text file PHP - Sistema de Rotas.pdf Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:18
This week:18
All time:11,353
This week:5Up