The class Route simulates a http request and response as would come from an API. The entry point routes.php in the base dir. Since this is not run using a webserver this code takes the cases in the nested and unnested routes and runs them through routes.php. To get and run the project
git clone https://github.com/parthamittra/routeClass.git
composer install
php routes.php
The requests (in the routes.php file with a Request object) will then output the json responses.
Using modern PHP (think PHP7, composer, autoloading, PSR-4, and best practices) please create a "Routing Class" as defined below.
A routing class is designed to take a url segment and automatically invoke a controller class. It does this by breaking up the url segments that come in a predictable pattern and invoking the correct class and method and inserting parameters into the method invoked. We will be following a REST-ful convention and be using common http methods. Your Routing class will be aware of the http method and the actual url segment.
Here is the complete requirement. These tables represents all possible routing logic.
Method | Segment | Controller class name | Controller method name | Parameters |
---|---|---|---|---|
GET | /patients | PatientsController | index | none |
GET | /patients/2 | PatientsController | get | this should invoke get($patientId) where $patientId = 2 |
POST | /patients | PatientsController | create | none (extra credit for handling the request body) |
PATCH | /patients/2 | PatientsController | update | update($patientId) |
DELETE | /patients/2 | PatientsController | delete | delete($patientId) |
Method | Segment | Controller class name | Controller method name | Parameters |
---|---|---|---|---|
GET | /patients/2/metrics | PatientsMetricsController | index | index($patientId) |
GET | /patients/2/metrics/abc | PatientsMetricsController | get | get($patientId, $metricId) |
POST | /patients/2/metrics | PatientsMetricsController | create | create($patientId) |
PATCH | /patients/2/metrics/abc | PatientsMetricsController | update | update($patientId, $metricId) |
DELETE | /patients/2/metrics/abc | PatientsMetricsController | delete | delete($patientId, $metricId) |
To invoke the Routing class we'll be calling it statically. For example we'll be defining a routes.php
that looks like this
Route::resource('patients');
Route::resource('patients.metrics');