Code Monkey home page Code Monkey logo

Comments (5)

bramus avatar bramus commented on July 29, 2024 3

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.

 avatar commented on July 29, 2024 1

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.

ovflowd avatar ovflowd commented on July 29, 2024

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.

RinkAttendant6 avatar RinkAttendant6 commented on July 29, 2024

@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.

ovflowd avatar ovflowd commented on July 29, 2024

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)

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.