PHP code example of sectoroverload2k / rest-router
1. Go to this page and download the library: Download sectoroverload2k/rest-router 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/ */
sectoroverload2k / rest-router example snippets
// File: /controllers/c_users.php
class Users extends Controller
{
public function __construct()
{
parent::__construct();
}
// GET /users/profile
public function get_profile()
{
// Return user profile
}
// PUT /users/profile
public function put_profile($data = [])
{
// Update user profile
}
}
class Customers extends Controller
{
// GET /customer/123
public function get_customer($params = [])
{
$customer_id = $params['id']; // "123"
// Fetch and return customer data
}
}
$router->addRule('/blog/:category/:post_id', array(
'controller' => 'blog',
'action' => 'get_post'
));
// Controller receives both parameters
public function get_post($params = [])
{
$category = $params['category'];
$post_id = $params['post_id'];
// Fetch and return blog post
}
// ✅ CORRECT - follows {verb}_{action} pattern
public function get_profile() { }
public function put_profile() { }
public function delete_profile() { }
public function post_login() { }
// ❌ WRONG - inconsistent naming
public function getProfile() { } // Wrong case
public function profile() { } // Missing verb
public function user_profile() { } // Doesn't match URL
// GET/DELETE - parameters from URL
public function get_item($params = [])
{
$id = $params['id'];
}
// POST/PUT - data from request body
public function post_create($data = [])
{
$name = $data['name'];
$email = $data['email'];
}
// ✅ CORRECT - explicit route for nested path
// routes.php:
$router->addRule('/users/profile/password', array(
'controller' => 'users',
'action' => 'put_profile_password'
));
// Controller:
public function put_profile_password($data = []) { }
// ❌ WRONG - relying on automatic routing for nested paths
// Will try to call put_profile($data['password']) instead
public function post_register($data = [])
{
// Validate {
// Return error response
return json_encode([
'error' => true,
'message' => 'Email and password are
switch($data->getMethod()) {
case 'get':
// Custom route with parameter
$router->addRule('/customer/:id', array(
'controller' => 'customers',
'action' => 'get_customer'
));
// Nested path
$router->addRule('/users/profile/settings', array(
'controller' => 'users',
'action' => 'get_profile_settings'
));
break;
case 'put':
// Nested paths (order matters!)
$router->addRule('/users/profile/password', array(
'controller' => 'users',
'action' => 'put_profile_password'
));
$router->addRule('/users/profile/settings', array(
'controller' => 'users',
'action' => 'put_profile_settings'
));
break;
case 'post':
// Add custom POST routes here
break;
case 'delete':
// Add custom DELETE routes here
break;
}
$router->init();
class Users extends Controller
{
public function __construct()
{
parent::__construct();
}
/**
* GET /users/profile
* Automatic routing - no explicit rule needed
*/
public function get_profile()
{
// Return user profile data
return json_encode([
'success' => true,
'data' => $profile
]);
}
/**
* PUT /users/profile
* Automatic routing - no explicit rule needed
*/
public function put_profile($data = [])
{
// Update user profile
return json_encode([
'success' => true,
'data' => $updatedProfile
]);
}
/**
* PUT /users/profile/password
* Requires explicit route in routes.php
*/
public function put_profile_password($data = [])
{
if (empty($data['current_password']) || empty($data['new_password'])) {
return json_encode([
'error' => true,
'message' => 'current_password and new_password are
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.