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 Improved] spark routes shows invalid routes #7419

Merged
merged 3 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public function read(string $class, string $defaultController = 'Home', string $
// Remove HTTP verb prefix.
$methodInUri = lcfirst(substr($methodName, strlen($httpVerb)));

// Check if it is the default method.
if ($methodInUri === $defaultMethod) {
$routeWithoutController = $this->getRouteWithoutController(
$routeForDefaultController = $this->getRouteForDefaultController(
$classShortname,
$defaultController,
$classInUri,
Expand All @@ -75,8 +76,11 @@ public function read(string $class, string $defaultController = 'Home', string $
$httpVerb
);

if ($routeWithoutController !== []) {
$output = [...$output, ...$routeWithoutController];
if ($routeForDefaultController !== []) {
// The controller is the default controller. It only
// has a route for the default method. Other methods
// will not be routed even if they exist.
$output = [...$output, ...$routeForDefaultController];

continue;
}
Expand Down Expand Up @@ -113,6 +117,12 @@ public function read(string $class, string $defaultController = 'Home', string $
$params[$param->getName()] = $required;
}

// If it is the default controller, the method will not be
// routed.
if ($classShortname === $defaultController) {
$route = 'x ' . $route;
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
}

$output[] = [
'method' => $httpVerb,
'route' => $route,
Expand Down Expand Up @@ -151,9 +161,11 @@ private function getUriByClass(string $classname): string
}

/**
* Gets a route without default controller.
* Gets a route for the default controller.
*
* @phpstan-return list<array>
*/
private function getRouteWithoutController(
private function getRouteForDefaultController(
string $classShortname,
string $defaultController,
string $uriByClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;

use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home;
use CodeIgniter\Test\CIUnitTestCase;
use Tests\Support\Controllers\Newautorouting;
use Tests\Support\Controllers\Remap;
Expand All @@ -22,13 +23,13 @@
*/
final class ControllerMethodReaderTest extends CIUnitTestCase
{
private function createControllerMethodReader(): ControllerMethodReader
{
private function createControllerMethodReader(
string $namespace = 'Tests\Support\Controllers'
): ControllerMethodReader {
$methods = [
'get',
'post',
];
$namespace = 'Tests\Support\Controllers';

return new ControllerMethodReader($namespace, $methods);
}
Expand Down Expand Up @@ -63,6 +64,41 @@ public function testRead()
$this->assertSame($expected, $routes);
}

public function testReadDefaultController()
{
$reader = $this->createControllerMethodReader(
'CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers'
);

$routes = $reader->read(Home::class);

$expected = [
0 => [
'method' => 'get',
'route' => '/',
'route_params' => '',
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::getIndex',
'params' => [],
],
[
'method' => 'post',
'route' => '/',
'route_params' => '',
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::postIndex',
'params' => [],
],
[
'method' => 'get',
'route' => 'x home/foo',
'route_params' => '',
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::getFoo',
'params' => [],
],
];

$this->assertSame($expected, $routes);
}

public function testReadControllerWithRemap()
{
$reader = $this->createControllerMethodReader();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers;

use CodeIgniter\Controller;

/**
* The default controller for Auto Routing (Improved)
*/
class Home extends Controller
{
public function getIndex()
{
}

public function postIndex()
{
}

/**
* This method cannot be accessible.
*/
public function getFoo()
{
}
}