Comments (3)
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.
"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.
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)
- phprouter works with subfolder in XAMMP but not in LAMP! HOT 4
- Why css file not working HOT 2
- group
- post method not working HOT 1
- Awesome router HOT 2
- Keeps redirecting to 404 page HOT 2
- optional params HOT 1
- Working in cpanel, not in my PC[ubuntu] HOT 3
- render index and 404 always in the end HOT 1
- .htaccess provided causes infinite loop in cloudflare HOT 1
- Problems with curl and certificates. HOT 1
- $_SESSION doesn't work HOT 1
- Query strings with multiple values HOT 2
- More documentation
- UTM tags and other option or unknown parameters HOT 1
- what if we want this library to use in an single project so we done have to put the files under the htdocs folder ? HOT 1
- PHP Jesus
- Undefined URL with multiple subfolders 404 Problem
- Nginx Configuration
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from main.