Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

adding multiple routing using a single path #1

Closed
wants to merge 0 commits into from

Conversation

codad5
Copy link

@codad5 codad5 commented Aug 1, 2022

Just like express.js router this is to add multiple routing methods for a single route path
just like

#multiple route 
$router->route('/hello')
    ->get(function($req, $res) {
        return $res->send("<h1>Hello World</h1>")->status(200); // sends html response
    })
    ->post(function($req, $res) {
        return $res->send([
            "message" => "Hello World posted"
        ])->status(200); // sends json response
    });

instead of

# GET route
$router->get("/hello", function($req, $res) {
    return $res->send("<h1>Hello World</h1>")->status(200); // sends html response
});

# POST route
$router->post("/hello", function($req, $res) {
   return $res->send([
            "message" => "Hello World posted"
        ])->status(200); // sends json response
});

README.md Outdated
@@ -51,7 +51,7 @@ use Trulyao\PhpRouter\Router as Router;
require(__DIR__ . '/../vendor/autoload.php');

# Create a new instance
$router = new Router(__DIR__."/views", "examples");
$router = new Router(__DIR__."/src", "examples");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The src folder is not where the view is at, this assumes they have autoloader mapped to /src too and the index.php is outside that, the initial version was written to match the examples folder. Check it out for reference.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@codad5 codad5 closed this Aug 1, 2022
src/Helper/Route.php Outdated Show resolved Hide resolved
@codad5 codad5 reopened this Aug 1, 2022
src/Router.php Outdated
@@ -123,23 +124,39 @@ private function add_route($path, $cb, $method = "GET")
}

# Create a GET route
public function get($path, $cb) {
public function get($path = null, $cb) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having null as a route is not user-friendly or visually pleasing, perhaps you could change it to simply take empty strings?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes . You are right. I notice that after creating the pull request.. sorry my bad

src/index.php Outdated
@@ -1,6 +1,6 @@
<?php
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, great you caught this 😅

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂 that was the first issue I ran into

@aosasona
Copy link
Owner

aosasona commented Aug 1, 2022 via email

@codad5
Copy link
Author

codad5 commented Aug 1, 2022

Had to give reference to the main route object in the sub route .. due to the $route->serve() method

.htaccess Outdated
@@ -0,0 +1,9 @@
Options +FollowSymLinks
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For dev testing using using the PHP server, this is not necessary and as I mentioned, it is all routed to /examples, you are redirecting all traffic here and it might try to look for /examples in /examples itself again.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants