A
Anonymous
Guest
https://github.com/thnguyendev/PHPWebCore
PHPWebCore is a MVC framework in PHP. It is built on the habits of using ASP.NET Core. It aims to be simple and easy to use. PHPWebCore implements PSR-7 HTTP message interfaces and PSR-17 HTTP Factories. It also supports dependency injection.
Web API
In this tutorial, we will create a PHPWebCore Web API app. First thing first, you need to create a PHPWebCore project.
When you have your project, define your API route that uses GET method. This api just simply returns the information of your project in JSON.
[project folder]/src/app/Route.php
Next step is creating ProjectController.php of the controller in "Controllers" folder. Set the response content type is application/json.
[project folder]/src/app/Controllers/ProjectController.php
It is almost done now. Use the routing and invoke action in your Bootstrap entry class then your app is ready to run.
[project folder]/src/app/Bootstrap.php
Is it too simple? Run your project and use below Url in a browser to see your work.
http://[your host]/project
PHPWebCore is a MVC framework in PHP. It is built on the habits of using ASP.NET Core. It aims to be simple and easy to use. PHPWebCore implements PSR-7 HTTP message interfaces and PSR-17 HTTP Factories. It also supports dependency injection.
Web API
In this tutorial, we will create a PHPWebCore Web API app. First thing first, you need to create a PHPWebCore project.
When you have your project, define your API route that uses GET method. This api just simply returns the information of your project in JSON.
[project folder]/src/app/Route.php
Code:
<?php
namespace App;
use PHPWebCore\AppRoute;
use PHPWebCore\RouteProperty;
use PHPWebCore\HttpMethod;
use App\Controllers\ProjectController;
class Route extends AppRoute
{
public function initialize()
{
$this->routes =
[
[
// Root path can be empty or "/"
RouteProperty::Path => "project",
// HTTP method attached to this action. If no declaration then all methods are accepted
RouteProperty::Methods => [HttpMethod::Get],
// Full class name with namespace. "App" is root namespace of the app
RouteProperty::Controller => ProjectController::class,
// Action method name
RouteProperty::Action => "getProjectInfo",
]
];
}
}
?>
Next step is creating ProjectController.php of the controller in "Controllers" folder. Set the response content type is application/json.
[project folder]/src/app/Controllers/ProjectController.php
Code:
<?php
namespace App\Controllers;
use PHPWebCore\Controller;
class ProjectController extends Controller
{
public function getProjectInfo()
{
// Set content type is application/json
header("Content-Type: application/json");
// Return json
echo json_encode([
'Project' => 'PHPWebCore Api Example',
'Framework' => 'PHPWebCore',
]);
}
}
?>
It is almost done now. Use the routing and invoke action in your Bootstrap entry class then your app is ready to run.
[project folder]/src/app/Bootstrap.php
Code:
<?php
namespace App;
use PHPWebCore\App;
class Bootstrap extends App
{
public function process()
{
// Use routing to map route
$this->useRouting(new Route());
// Invoke the action to fulfill the request
// Data likes user information from Authorization can be passed to controller by bucket
$this->invokeAction(bucket: null);
}
}
?>
Is it too simple? Run your project and use below Url in a browser to see your work.
http://[your host]/project