PHP framework for REST services.
Clone/Download this repo inside htdocs
folder for Xampp or www
folder for Wamp and then install its dependencies with Composer.
# clone this repo
$ git clone https://github.com/felipecespedes/viwell-framework.git viwell-framework
# change directory to your app
$ cd viwell-framework
# install dependencies with composer
$ composer install
Navigate to localhost/viwell-framework/public/ and if everything goes well you should get a welcome message like this:
{"message":"Welcome to Viwell Framework"}
If you are not using Xampp nor Wamp make sure to put the framework folder in place where your PHP server can reach it and adjust the url based on your server configurations.
- PHP >= 5.4
- PDO PHP Extension
- mod_rewrite enabled
- Composer
.
├── app
| ├── controllers # Folder for controllers
| ├── http
| | └── routes.php # File for routes
| └── models # Folder for models
├── config
| └── config.php # File for configurations
├── lib # Folder for framework core files
├── public # Folder for public files
├── LICENSE
├── README.md
└── composer.json # Composer file
Define app configurations in config.php
return [
"driver" => "mysql",
"hostname" => "localhost",
"port" => 3306,
"database" => "test",
"username" => "username",
"password" => "password",
"charset" => "utf8",
"collation" => "utf8_unicode_ci",
"prefix" => "",
"debug" => true
];
In routes.php you can define app routes that respond to different HTTP requests.
Router::get("/users/", "UserController", "index");
Router::get("/users/{id}", "UserController", "show");
Router::post("/users/", "UserController", "create");
Router::put("/users/{id}", "UserController", "update");
Router::delete("/users/{id}", "UserController", "delete");
Controllers go inside the app/controllers
folder.
Let's create a basic controller which uses the above defined routes:
<?php
namespace app\controllers;
use core\Controller;
use core\Response;
use core\Request;
use app\models\User;
class UserController extends Controller {
public function index() {
$users = User::all();
return Response::json($users);
}
public function show($id) {
$user = User::find($id);
if ( ! is_null($user)) {
return Response::json($user);
} else {
return Response::json([
"message" => "user does not exists"
], 400);
}
}
public function create() {
$user = new User();
$user->name = Request::body("name");
$user->age = Request::body("age");
$user->email = Request::body("email");
try {
$user->save();
return Response::json([
"message" => "user successfully created",
"user_id" => $user->id
]);
} catch (\Exception $e) {
return Response::json([
"message" => "user could not be created"
], 400);
}
}
public function update($id) {
$user = User::find($id);
$user->name = Request::body("name");
$user->age = Request::body("age");
$user->email = Request::body("email");
try {
$user->save();
return Response::json([
"message" => "user successfully updated"
]);
} catch (\Exception $e) {
return Response::json([
"message" => "user could not be updated"
], 400);
}
}
public function delete($id) {
$user = User::find($id);
try {
$user->delete();
return Response::json([
"message" => "user successfully deleted"
]);
} catch (\Exception $e) {
return Response::json([
"message" => "user could not be deleted"
], 400);
}
}
}
Models go inside the app/models
folder.
Models use Eloquent
To retrieve data given in a query string parameter, use:
$name = Request::param("name");
To retrieve all the data given in the query string parameters as an associative array, use
$params = Request::params();
To retrieve data given in the request body, use:
$name = Request::body("name");
To retrieve data given in a header, use:
$auth = Request::header("Authorization");
To retrieve all the data given in the headers as an associative array, use
$headers = Request::headers();
Response::json
Outputs a response with JSON formatted data, it takes 2 parameters:
- $data Data that can be converted into JSON format. required: true
- $statusCode Response status code. type: int | required: false | default: 200
public function index() {
$data = ["message" => "Welcome to Viwell Framework"];
return Response::json($data, 200);
}
Results in:
{"message":"Welcome to Viwell Framework"}
Response::header
Adds headers to the HTTP response, it takes 1 parameter:
- $header The header to be added. type: string | required: true
public function index() {
$users = User::all();
return Response::json($users)
->header("Allow: GET")
->header("Access-Control-Allow-Origin: *");
}
Response::withHeaders
Adds an array of headers to the HTTP response, it takes 1 parameter:
- $headers The headers to be added. type: string[] | required: true
public function index() {
$users = User::all();
return Response::json($users)
->withHeaders(["Allow: GET", "Access-Control-Allow-Origin: *"]);
}
Response::response
Outputs a response of any kind, could be used to outputs HTML responses, it takes 3 parameters:
- $data Outputted data. required: true
- $statusCode Response status code. type: int | required: false | default: 200
- $isJSON Whether the response is JSON or not. type: boolean | required: false | default: true
public function index() {
$html = "
<html>
<head></head>
<body>
<h1>Hello World</h1>
</body>
</html>
";
return Response::response($html, 200, false);
}
- Felipe Céspedes - felipecespedespisso@gmail.com - felipecespedes
If you found this project helpful and want to sponsor me for creating and sharing more educational open-source projects, please consider buying me a cup of coffee
This project is licensed under the MIT License - see the LICENSE file for details