How to remove duplication of procedural code?

Writing api in php using slim framework. Using GET - values produced for different entities stored in the database, and using a POST - change. Accordingly, the difference between all of these entities table in the database, the other fields and possible to implement some logic: if the value field < n, you cannot change or change in another place.

All the code is the template where XClassName different title classes:

interface iXClassName
 public function get_permition_for_operation();
 public function validate_input_get_array( $some_input_param);
 public function get_value_to_rend();

XClassName class implements iXClassName{


require 'Slim/Slim.php';

$app = new \Slim\Slim();

$app--->get('/api/get/XClassName/(:id)', function ($id) use ($app,$DBH) {

 $XClassName->validate_input_get_array( $some_input_param);

 return $app->render('template_get_XclassName.tml', $_params_array ));

$app->post('/api/post/XClassName/(:id)', function ($id) use ($app,$DBH) {

 $_post_array=array('XparamName'=> $app->request->post('paramName'), 'Yparam'=> $app->request->post('paramY'),...);

 return $app->redirect('/api/post/XClassName/');



Accordingly, when there is a new function you need to implement all of the api calls is placed inside each block $app->post() / $app->get() - you can forget/make a mistake

I would like to hear thoughts on the proper design of such a service.

PS: I think to replace it all pattern Strategy, but is it right?

October 3rd 19 at 02:12
2 answers
October 3rd 19 at 02:14

How would You like the opportunity?

Mean ? <?php function step1() { echo "This is middleware!"; } function step2() { echo "This is middleware!"; } function step3() { echo "This is middleware!"; } $app = new \Slim\Slim(); $app--->get('/foo', 'step1', 'step2', 'step3', function () { //Do something }); + to use the dynamic of ranting and describe all in one call of the Factory, which, depending on the router will instantiate the classes with a specific implementation? - kathlyn_Hudson commented on October 3rd 19 at 02:17
So, ie depending on the route parameters to pass to those functions that are needed inside: get_permition_for_operation all the rest - on request. A factory or singleton is details. But the router forget to pass this function. So look for another slim.before.router, it may be possible to describe all in one place. - nola_Christians commented on October 3rd 19 at 02:20
Thank you - kathlyn_Hudson commented on October 3rd 19 at 02:23
October 3rd 19 at 02:16

Code duplication is removed is very simple - duplicate chunks are in the function/procedure and variable are passed in as arguments. Especially here to help the lambda function (as far as I remember, PHP has an equivalent, as a minimum, the call-by-name).

Find more questions by tags PHP