Git Product home page Git Product logo

Comments (3)

Orio-nn avatar Orio-nn commented on August 23, 2024

are you talking about these line?

function get($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'GET' ){ route($route, $path_to_include); }  
}
function post($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'POST' ){ route($route, $path_to_include); }    
}
function put($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'PUT' ){ route($route, $path_to_include); }    
}
function patch($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'PATCH' ){ route($route, $path_to_include); }    
}
function delete($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'DELETE' ){ route($route, $path_to_include); }    
}

i'm sure there are better ways to write the chunk above but they don't all do the same thing.

get('/', 'example.php');
post('/', 'example.php');

these 2 functions are not the same although they return the same output. 1 checks for a get method and the other checks for a post. you can see this in the if statements inside these functions...

from main.

benanamen avatar benanamen commented on August 23, 2024

"1 checks for a get method and the other checks for a post."

I understand that, but every REQUEST METHOD, whether it be POST, GET or any of the other ones, they all call the route function (route($route, $path_to_include);) so it makes no difference what the REQUEST METHOD is.

"they don't all do the same thing."

Actually, they do all do the same thing. It doesn't matter what the REQUEST METHOD is.

As written, all those functions are just pointless wrappers to the route function.

Other than the function names and the method check, the code is EXACTLY the same and calls the EXACT same route function. The route method is where differences happen based on what is passed in from $route & $path_to_include.

You may as well just call the route function directly as it is written.

For a "proof of concept" you can use a tool like Postman and make a request using every REQUEST METHOD and you will get the exact same result.

You can also use the Firefox Browser Dev Tools to edit and resend the request using different request methods.

For an example use : http://main.test/user/5
Every request method will return: USER IN VIEWS WITH ID: 5

This should make the redundancy clearer. (replaced distinct function names and distinct request methods)

function X($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'X' ){ route($route, $path_to_include); }  
}
function X($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'X' ){ route($route, $path_to_include); }    
}
function X($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'X' ){ route($route, $path_to_include); }    
}
function X($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'X' ){ route($route, $path_to_include); }    
}
function X($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'X' ){ route($route, $path_to_include); }    
}

from main.

phprouter avatar phprouter commented on August 23, 2024

You are right. The code is redundant, though it gives a very clear overview of the routes and also prevents for example doing a POST when a GET was needed.

from main.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.