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

fix: auto routing legacy and $route->add() #7690

Merged

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Jul 10, 2023

Description
See https://forum.codeigniter.com/showthread.php?tid=86977

There is a bug:

  • When you enable Auto Routing Legacy, if you $route->add(), the controller's other methods are inaccessible from the web browser.

How to Test

--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -19,7 +19,7 @@ $routes->set404Override();
 // where controller filters or CSRF protection are bypassed.
 // If you don't want to define all routes, please use the Auto Routing (Improved).
 // Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
-// $routes->setAutoRoute(false);
+$routes->setAutoRoute(true);
 
 /*
  * --------------------------------------------------------------------
@@ -31,6 +31,8 @@ $routes->set404Override();
 // route since we don't have to scan directories.
 $routes->get('/', 'Home::index');
 
+$routes->add('instructor/login', 'Instructor::login');
+
 /*
  * --------------------------------------------------------------------
  * Additional Routing
<?php

namespace App\Controllers;

class Instructor extends BaseController
{
    public function login()
    {
        return __METHOD__;
    }

    public function logout()
    {
        return __METHOD__;
    }
}

Navigate to:

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis force-pushed the fix-auto-routing-legacy-cli-routes branch from ed8bfd6 to 72a32c2 Compare July 10, 2023 00:58
Comment on lines 129 to 155

foreach ($this->protectedControllers as $controllerInRoute) {
if (! is_string($controllerInRoute)) {
continue;
}
if (strtolower($controllerInRoute) !== $controller) {
continue;
$methodName = strtolower($this->methodName());

foreach ($this->cliRoutes as $handler) {
if (is_string($handler)) {
$handler = strtolower($handler);

if (strpos($handler, $controller . '::' . $methodName) === 0) {
throw new PageNotFoundException(
'Cannot access CLI Route: ' . $uri
);
}

if ($handler === $controller) {
throw new PageNotFoundException(
'Cannot access CLI Route: ' . $uri
);
}
}

throw new PageNotFoundException(
'Cannot access the controller in a CLI Route. Controller: ' . $controllerInRoute
);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I restored the logic from v4.1.9:

if ($this->collection->getHTTPVerb() !== 'cli') {
$controller = '\\' . $defaultNamespace;
$controller .= $this->directory ? str_replace('/', '\\', $this->directory) : '';
$controller .= $controllerName;
$controller = strtolower($controller);
$methodName = strtolower($this->methodName());
foreach ($this->collection->getRoutes('cli') as $route) {
if (is_string($route)) {
$route = strtolower($route);
if (strpos($route, $controller . '::' . $methodName) === 0) {
throw new PageNotFoundException();
}
if ($route === $controller) {
throw new PageNotFoundException();
}
}
}
}

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 10, 2023
@kenjis kenjis marked this pull request as draft July 10, 2023 02:27
@kenjis kenjis added the breaking change Pull requests that may break existing functionalities label Jul 10, 2023
@kenjis kenjis force-pushed the fix-auto-routing-legacy-cli-routes branch from 6dffdac to 94843ff Compare July 10, 2023 04:42
@kenjis kenjis marked this pull request as ready for review July 10, 2023 04:42
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

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

One naming comment, I'm fine either way.

system/Router/RouteCollection.php Outdated Show resolved Hide resolved
@kenjis kenjis force-pushed the fix-auto-routing-legacy-cli-routes branch from 94843ff to 3c9cde7 Compare July 12, 2023 04:21
@kenjis kenjis force-pushed the fix-auto-routing-legacy-cli-routes branch from 3c9cde7 to bed018a Compare July 12, 2023 05:16
Copy link

@TimexPeachtree TimexPeachtree left a comment

Choose a reason for hiding this comment

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

Awesome looking forward ⏩ to 4.3.7

@kenjis kenjis merged commit b84bd14 into codeigniter4:develop Jul 16, 2023
51 checks passed
@kenjis kenjis deleted the fix-auto-routing-legacy-cli-routes branch July 16, 2023 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Pull requests that may break existing functionalities bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants