Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"mount" one by one #156

Open
milkamil93 opened this issue Aug 2, 2021 · 4 comments
Open

"mount" one by one #156

milkamil93 opened this issue Aug 2, 2021 · 4 comments

Comments

@milkamil93
Copy link

How do I use "mount" one by one? The last one cancels the previous one.

$router->mount('/api/v1', function () use ($router) {}); doesn't work
$router->mount('/api/v2', function () use ($router) {}); works

@uvulpos
Copy link
Collaborator

uvulpos commented Aug 3, 2021

Seems to work using Bramus version 1.6 and PHP 7.4.21
(if your project is not up-to-date, try ⇾ composer update)

<?php
// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

// Create Router instance
$router = new \Bramus\Router\Router();

$router->mount('/api/v1', function () use ($router) {
  $router->get('/', function() { echo 'API version 1 overview'; });
  $router->get('add', function() { echo 'API version 1 add something'; });
});

$router->mount('/api/v2', function () use ($router) {
  $router->get('/', function() { echo 'API version 2 overview'; });
  $router->get('add', function() { echo 'API version 2 add something'; });
});

$router->set404(function() {
    header('HTTP/1.1 404 Not Found');
    echo "404";
});

// Run it!
$router->run();

@milkamil93
Copy link
Author

Sorry, I forgot one small detail, but very important

$router->mount('/api/v1', function () use ($router) {
  $router->setNamespace('Api\v1'); // !!!
  $router->get('/', function() { echo 'API version 1 overview'; });
  $router->get('add', function() { echo 'API version 1 add something'; });
});

$router->mount('/api/v2', function () use ($router) {
  $router->setNamespace('Api\v2'); // !!!
  $router->get('/', function() { echo 'API version 2 overview'; });
  $router->get('add', function() { echo 'API version 2 add something'; });
});

The second "setNamespace" overrides the previous one and ends up with the wrong path to the class

@uvulpos
Copy link
Collaborator

uvulpos commented Aug 4, 2021

You're right. The current project structure does not allow that because setNamespace defines just a class-variable. To fix that, you need to move getNamespace. Please consider making a pull request to support the project 😉

@milkamil93
Copy link
Author

milkamil93 commented Aug 5, 2021

You're right. The current project structure does not allow that because setNamespace defines just a class-variable. To fix that, you need to move getNamespace. Please consider making a pull request to support the project 😉

#158

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

No branches or pull requests

2 participants