Comments (5)
Whilst I like the idea of making sub requests, it would – if you want to cover all bases (which Silex/Symfony does) – necessitate a Request
class (and quite possibly a Response
class too), as you'd want to override the contents of $_GET
, $_POST
, $_SESSION
, and $_COOKIE
for the sub request (and then revert back to the original values after said sub request has been made).
Not covering all bases doesn't seem a viable/proper option to me.
Take this example (as provided at the top of this thread):
$router->get('/hello/(\w+)', function ($name) {
echo "Hello $name";
});
$router->call('GET', '/hello/Tyler'); // should echo "Hello Tyler"
It indeed will work as it only relies on dynamic route patterns.
But this example below won't work properly, as it relies not only on dynamic route patterns but also on $_POST
, etc.
$router->post('/subscribe', function () {
MailingList::subscribe(Request::postValue('email'));
});
$router->get('/some/url', function () {
$router->call('POST', '/subscribe'); // pass call to `/subscribe` route
});
My preference would be to cover all bases, yet then it will exceed the scope of the router itself.
~
Circling back to your original problem the by you suggested solution 2 (viz. making a local request) looks a viable one to me:
- If you make requests to the local machine (viz. 127.0.0.1, or a name that resolves to that) you won't have that much (network traffic) overhead I guess (testing needed though).
- Making a new request will enforce correct data to be found in
$_GET
and$_POST
(and$_COOKIE
+$_SESSION
if you use a cookie jar and pass the session id with the new request).
Regards,
Bramus.
from router.
A good idea. While there is not this method, I think you can code something like this:
$router->get('/hello/(\w+)', function ($name) {
echo "Hello $name";
});
$_SERVER['REQUEST_URI'] = 'hello/Tyler';
$router->run();
from router.
A nice idea. But that kills the concept of a Router.
In fact, we can add that, with the approval of @bramus, but is necessary that the call method be linked with a node.
A nice idea and i will be happy to code that, if Bramus accept.
from router.
@Sant0ro Thank you for your consideration.
My current use case is to invoke a private, local API that uses this library to handle the routing. The only way I can think of to access the API is to (1) move all the API callbacks to class methods, instantiate the required class as necessary, and call the method both from the API and the application or (2) make a local HTTP request to the API.
Option 1 increases the coupling of the application to the API and option 2 seems like a lot of overhead to use the network for something running locally.
from router.
You don't need instantiate each Callback. Create a Singleton Pattern with a Factory Pattern.
Doing a union of those, you will get exactly what you need. A Response Handler that "redirects" to a specific Handler and "invoke" it using Singleton Lazy access. (Static Binding of getInstance()). You only need have a base Interface and a Singleton Model. Your handlers can extends this Model.
Good luck.
from router.
Related Issues (20)
- i like it and use in my framework (Flare ) 4.0.1
- $route->set404 = redirect to ?uri= if folder exist HOT 2
- Any HTTP code handling? HOT 3
- Multilingual default route without lang
- Generating URLs HOT 1
- Form submits a GET request without a submit-button HOT 1
- um... controller blank page HOT 1
- how to run php file with command .for example: php index.php Home index HOT 1
- How can i serve the static folder using Router? HOT 1
- $match arrgument not being used anywhere
- after router middleware not keeping states?
- How can I setup this middleware? HOT 1
- router in subfolder HOT 2
- There is no Cache funciton! so Please Add Caching Feature! HOT 1
- 404 error executing more than twice
- No executing middleware HOT 2
- I have uploaded my system that works with this router to the hosting, and it's not functioning. HOT 1
- Post values not coming
- Accessors and named routes
- How to do the following HOT 4
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 router.