1. Go to this page and download the library: Download thnguyendev/phpwebcore 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/ */
thnguyendev / phpwebcore example snippets
namespace App;
use PHPWebCore\AppRoute;
use PHPWebCore\RouteProperty;
use App\Controllers\HomeController;
class Route extends AppRoute
{
public function initialize()
{
$this->routes =
[
[
// Root path can be empty or "/"
RouteProperty::Path => "",
// Full class name with namespace. "App" is root namespace of the app
RouteProperty::Controller => HomeController::class,
// Action method name
RouteProperty::Action => "index",
// View file name with full path. The root is "app" folder
RouteProperty::View => "Views/HomeView",
],
[
// Root path can be empty or "/"
RouteProperty::Path => "/",
// Parameters is an a array of string, contains all parameters' names
RouteProperty::Parameters => ["name"],
// Full class name with namespace. "App" is root namespace of the app
RouteProperty::Controller => HomeController::class,
// Action method name
RouteProperty::Action => "index",
// View file name with full path. The root is "app" folder
RouteProperty::View => "Views/HomeView",
],
];
}
}
namespace App\Controllers;
use PHPWebCore\Controller;
class HomeController extends Controller
{
public function index(string $name = null)
{
$this->view(["name" => $name]);
}
}
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);
}
}
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",
]
];
}
}
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',
]);
}
}
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);
}
}
namespace App;
use PHPWebCore\ErrorServiceInterface;
class ExceptionHandler implements ErrorServiceInterface
{
public function process(\Throwable $e)
{
echo "I've got this exception for now.";
}
}
namespace App;
use PHPWebCore\App;
use PHPWebCore\ErrorServiceInterface;
class Bootstrap extends App
{
public function process()
{
$this->container = $this->container
->withTransient(ErrorServiceInterface::class, ExceptionHandler::class);
throw new \Exception();
}
}
namespace App\Services;
use RedBeanPHP\R;
class DatabaseService
{
public const Project = "porject";
public const ProjectName = "name";
public const ProjectFramework = "framework";
public function __construct(string $connectionString)
{
// Open a connection
R::setup($connectionString);
$project = R::load(static::Project, 1);
// No database then create one
if ($project["id"] === 0)
{
$project = R::dispense(static::Project);
$project[static::ProjectName] = "PHPWebCore with RedBeanPHP and SQLite";
$project[static::ProjectFramework] = "PHPWebCore";
R::store($project);
}
}
public function close()
{
// Close the connection
R::close();
}
}
namespace App\Services;
interface ProjectServiceInterface
{
public function getProjectInfo();
}
namespace App\Services;
use PHPWebCore\NotFoundException;
use RedBeanPHP\R;
class ProjectService implements ProjectServiceInterface
{
public function getProjectInfo()
{
$project = R::load(DatabaseService::Project, 1);
if ($project["id"] === 0)
throw new NotFoundException("Project not found", 404);
return json_encode($project);
}
}
namespace App\Controllers;
use PHPWebCore\Controller;
use App\Services\ProjectServiceInterface;
class ProjectController extends Controller
{
private $projectService;
public function __construct(ProjectServiceInterface $projectService)
{
$this->projectService = $projectService;
}
public function getProjectInfo()
{
// Set content type is application/json
header("Content-Type: application/json");
// Return json
echo $this->projectService->getProjectInfo();
}
}
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",
]
];
}
}
namespace App;
use App\Services\DatabaseService;
use App\Services\ProjectServiceInterface;
use App\Services\ProjectService;
use PHPWebCore\App;
class Bootstrap extends App
{
public function process()
{
// Initialize Database
$db = new DatabaseService("sqlite:".static::getAppFolder()."/Project.db");
// Add services to container
$this->container = $this->container
->withTransient(ProjectServiceInterface::class, ProjectService::class);
// 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);
// Close Database connection
$db->close();
}
}
namespace App\Services;
interface UserServiceInterface
{
public function login($username, $password);
public function authorize();
}
namespace App\Services;
use Psr\Http\Message\ServerRequestInterface;
use Firebase\JWT\JWT;
class UserService implements UserServiceInterface
{
private const key = "secret key";
private $request;
public function __construct(ServerRequestInterface $request)
{
$this->request = $request;
}
public function login($username, $password)
{
if (!is_string($username) || $username !== "username")
throw new \Exception("Invalid username", 400);
if (!is_string($password) || $password !== "password")
throw new \Exception("Invalid password", 400);
$time = time();
$payload =
[
'iss' => "PHP-JWT",
'iat' => $time,
'nbf' => $time + 10,
'exp' => $time + 600,
'user' => $username,
];
return JWT::encode($payload, $this::key);
}
public function authorize()
{
$query = $this->request->getQueryParams();
$token = "";
if (isset($query["token"]))
$token = $query["token"];
try
{
return JWT::decode($token, $this::key, array("HS256"));
}
catch (\Throwable $e)
{
throw new \Exception($e->getMessage(), 403);
}
}
}
namespace App\Controllers;
use PHPWebCore\Controller;
use App\Services\UserServiceInterface;
class UserController extends Controller
{
private $userService;
public function __construct(UserServiceInterface $userService)
{
$this->userService = $userService;
}
public function login($username, $password)
{
$token = $this->userService->login($username, $password);
echo "token = {$token}";
}
public function getUserInfo()
{
header("Content-Type: application/json");
if (isset($this->bucket["user"]))
echo json_encode($this->bucket["user"]);
}
}
namespace App;
use PHPWebCore\AppRoute;
use PHPWebCore\RouteProperty;
use PHPWebCore\HttpMethod;
use App\Controllers\UserController;
class Route extends AppRoute
{
public function initialize()
{
$this->routes =
[
[
// Root path can be empty or "/"
RouteProperty::Path => "login",
// Parameters is an a array of string, contains all parameters' names
RouteProperty::Parameters => ["username", "password"],
// 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 => UserController::class,
// Action method name
RouteProperty::Action => "login",
],
[
// Root path can be empty or "/"
RouteProperty::Path => "user",
// 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 => UserController::class,
// Action method name
RouteProperty::Action => "getUserInfo",
// If true, this action need to be authorized
RouteProperty::Authorized => true,
],
];
}
}
namespace App;
use Psr\Http\Message\ServerRequestInterface;
use PHPWebCore\App;
use PHPWebCore\RouteProperty;
use App\Services\UserServiceInterface;
use App\Services\UserService;
class Bootstrap extends App
{
public function process()
{
// Add services to container
$this->container = $this->container
->withSingleton(ServerRequestInterface::class, $this->request)
->withTransient(UserServiceInterface::class, UserService::class);
// Use routing to map route
$this->useRouting(new Route());
$bucket = [];
// Authorize here
if (isset($this->route[RouteProperty::Authorized]) && $this->route[RouteProperty::Authorized])
{
$userService = $this->container->get(UserServiceInterface::class);
$bucket["user"] = $userService->authorize();
}
// Invoke the action to fulfill the request
// Data likes user information from Authorization can be passed to controller by bucket
$this->invokeAction(bucket: $bucket);
}
}
nginx
root <path to project folder>/public;
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?q=$1;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.