Download the PHP package thnguyendev/phpwebcore without Composer
On this page you can find all versions of the php package thnguyendev/phpwebcore. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thnguyendev/phpwebcore
More information about thnguyendev/phpwebcore
Files in thnguyendev/phpwebcore
Informations about the package phpwebcore
PHPWebCore 1.0.0
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.
PHPWebCore is a very basic framework. But, you can always include any PHP packages that need for your app, for instance, RedBeanPHP, Doctrine ORM, Firebase PHP-JWT, php-amqplib, etc...
Quick start
- PHPWebCore needs Composer and of course PHP. Make sure you download and install PHP and Composer.
-
Create PHPWebCore project by Composer. Then, run the update command from Composer to download all of denpendencies.
-
The web root folder is "public" in project folder. There are several ways to run the app: use the PHP built-in server, Apache server or Nginx server, etc.. For PHP built-in server, you just need to set the document root is "public" folder. In Apache server, .htaccess file is ready in "public" folder, you need to set "public" folder is Apache web root directory. If you use Nginx server, you need to add a server in nginx.conf which has root points to "public" folder in app and setup location like below.
-
PHP built-in server
-
Apache server .htaccess config
- Nginx server nginx.conf config
-
-
Now back to the app, your workspace in the app is just inside the "src/app" folder. Working with the routes of web app is our first step. PHPWebCore does not use the PHP attributes for the routing. The default routing is the Route class extends from PHPWebCore\AppRoute in Route.php. You need to implement initialize() method for Route class. Routes should be defined here. The request Url paths split into paths and parameters. PHPWebCore will map it to the first route that has the most segments in path. In this example, we create 2 routes: one is the root path and the other is also the root path but it has "name" as parameter.
- [project folder]/src/app/Route.php
-
As you see, the routes need HomeController class with the method index(). A controller class can have any name that you like but it must be derived from PHPWebCore/Controller class. The name "HomeController" comes from ASP.NET Core. Moreover, the index() method can take 1 argument or nothing at all. The index() method will call view() method and pass "name" to $args. Now, we create a folder name "Controllers" inside "app" folder and create a file "HomeController.php". Please note that the name of the php file must be the same as the class name.
- [project folder]/src/app/Controllers/HomeController.php
-
The routes also need a view for the controller. It recommends to use HTML or PHP for the view file. You could put PHP codes inside your HTML template. According to our declaration in routers, the app will look for the view "HomeView", "HomeView.php" or "HomeView.html" in "Views" folder inside "app" folder. So, we create "Views" folder inside "src/app", then create "HomeView.php" inside "Views" folder.
- [project folder]/src/app/Views/HomeView.php
-
So, everything is ready except the last step, the app entry point. Default PHPWebCore app entry class is Bootstrap, which is devired from PHPWebCore/App. Yes, it is Bootstrap instead of Startup. Did you feel the ASP.NET Core until now :D? We need to implement process() method. We will make flow of processing of the app here, such as add servcies to app's container, redirect to HTTPS, allow CORS (origin only), use routing, invoke action, etc... You can also run middlewares here, before and after invoke action like authorization. In this example, we only use routing and invoke action after that.
- [project folder]/src/app/Bootstrap.php
- Finally, your first PHPWebCore app is ready. Run your app and try it. Use the following Urls in your browser.
- http://[your host]
- http://[your host]/[name]
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
Override exception handler
This shows how to have your own way to deal with the errors in your app. From your app antry point, the PHPWebCore\ErrorServiceInterface takes care of the exceptions in your app. So, to handle errors by yourself, just have your class implement ErrorServiceInterface and override it in app's container. The interface just need a method name process() with a Throwable parameter.
- http://[your host]/project
-
Create your own exception handler and override the default
-
[project folder]/src/app/ExceptionHandler.php
- [project folder]/src/app/Bootstrap.php
-
- Run your project and open below Url in browser.
- http://[your host]
RedBeanPHP and SQLite
This example desmonstrates how your PHPWebCore app work with databases. We use RedBeanPHP and SQLite because it is so easy to included to your app.
- http://[your host]
-
Before we continue, let's make sure you have SQLite enabled in PHP, created your PHPWebCore and update composer.json to include RedBeanPHP to your app then run update command from Composer
-
php.ini
-
[project folder]/composer.json
- Run Composer update command
-
-
We build 2 services for our app, one is DatabaseService to work with SQLite. It creates a connection and initializes the database in the constructor. It also provides a method to stop the connection before the app stops. The other service is ProjectService which provides the data from database. We use this service as a dependency injection of the controller, so we need to build an interface for this service. You need to create "Services" folder in your app folder to put all of these servies in.
-
[project folder]/src/app/Services/DatabaseService.php
-
[project folder]/src/app/Services/ProjectServiceInterface.php
- [project folder]/src/app/Services/ProjectService.php
-
-
The last step we create a controller, declare a route and update the Bootstrap. Beside use routing and invoke controller action in Boostrap, we need to add ProjectService to app's container, initialize the database and close it before app stops.
-
[project folder]/src/app/Controllers/ProjectController.php
-
[project folder]/src/app/Route.php
- [project folder]/src/app/Bootstrap.php
-
- Now your PHPWebCore app is ready to run. When the first request send to your app. It will creates a SQLite file name "Project.db" in your app folder. Try the following Url
- http://[your host]/project
Firebase PHP-JWT authorization
This time we make PHPWebCore app work with PHP-JWT authorization.
- http://[your host]/project
-
We create new PHPWebCore project and add Firebase PHP-JWT in
-
[project folder]/composer.json
- Run Composer update command
-
-
We build UserService to provide 2 functions are login() and authorize(). The login() method needs 2 parameters are $username and $password and it generates a token if $username and $password are matched with "username" and "password". The authorize() finds a $token from query string and return payload if $token is valid. We don't use Authorization header in request but the query string so that we can use the token in Url.
-
[project folder]/src/app/Services/UserServiceInterface.php
- [project folder]/src/app/Services/UserService.php
-
-
We also create a controller has 2 action methods login() and getUserInfo(). The login() method get 2 parameters $username and $password from the Url path rather then from POST data so we can test it in a browser easily. The other getUserInfo() method need to be authorized and print the payload from the valid token.
- [project folder]/src/app/Controllers/UserController.php
-
Now we just need to declare routes and configure app entry point then it's done.
-
[project folder]/src/app/Route.php
- [project folder]/src/app/Bootstrap.php
-
- Run your PHPWebCore app and use the login Url to get a token. Then, we use the token in user Url to get the payload from the token.
- http://[your host]/login/username/password
- http://[your host]/user?token=[token]