codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: Wrong filter in route #7527

Closed fdjrr closed 1 year ago

fdjrr commented 1 year ago

PHP Version

8.1

CodeIgniter4 Version

4.3.5

CodeIgniter4 Installation Method

Composer (as dependency to an existing project)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

No response

What happened?

I used the resources function with a different filter, but after I used the php spark routes command, the filter applied was the same. Is it a bug? or am I using the wrong resource? Please help.

Screenshot 2023-05-29 110550

the psychotests/answer routing should only be cors, no is_admin.

Screenshot 2023-05-29 110559

Screenshot 2023-05-29 110758

Steps to Reproduce

Please give me a solution.

Expected Output

The filter should be different

psikotests -> is_admin, cors, invalidchars psikotests/answers -> cors, invalidchars

Anything else?

No response

kenjis commented 1 year ago

Don't use screenshots for code. Because we cannot reuse them. Please copy and paste your code, and show the minimum whole code to work, not a part of it that does not work.

fdjrr commented 1 year ago

// Filter : is_admin, CORS
$routes->group('api', static function ($routes) {
  $routes->group('v1', [
    'namespace' => 'App\Controllers\API\v1',
    'filter' => ['is_admin', 'cors']
  ], static function ($routes) {
$routes->resource('psikotests', ['controller' => 'PsikotestsController']);
  });
});

// Filter : CORS not is_admin
$routes->group('api', static function ($routes) {
  $routes->group('v1', [
    'namespace' => 'App\Controllers\API\v1',
    'filter' => 'cors',
  ], static function ($routes) {
$routes->resource('psikotests/answers', ['controller' => 'PsikotestAnswersController']);
  });
});

Result :

Screenshot 2023-05-29 110550

iRedds commented 1 year ago

You need to define the routes for the PsikotestAnswersController before you define the routes for the PsikotestController. Or change the order in which routes are processed by lowering the priority for PsikotestController routes. https://codeigniter.com/user_guide/incoming/routing.html#changing-route-priority

Because the address psikotests/answers matches the pattern psikotests/(.*)

kenjis commented 1 year ago

As iRedds says, your routes are defined incorrectly.

The first entry that matches the current URI will be served. The route GET api/v1/psikotests/(.*) takes all routes starting with api/v1/psikotests/answers.

+--------+-------------------------------------+------+---------------------------------------------------------------+----------------+-----------------------+
| Method | Route                               | Name | Handler                                                       | Before Filters | After Filters         |
+--------+-------------------------------------+------+---------------------------------------------------------------+----------------+-----------------------+
| GET    | /                                   | »    | \App\Controllers\Home::index                                  |                | toolbar               |
| GET    | api/v1/psikotests                   | »    | \App\Controllers\API\v1\PsikotestsController::index           | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/new               | »    | \App\Controllers\API\v1\PsikotestsController::new             | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/(.*)/edit         | »    | \App\Controllers\API\v1\PsikotestsController::edit/$1         | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/(.*)              | »    | \App\Controllers\API\v1\PsikotestsController::show/$1         | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/answers           | »    | \App\Controllers\API\v1\PsikotestAnswersController::index     | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/answers/new       | »    | \App\Controllers\API\v1\PsikotestAnswersController::new       | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/answers/(.*)/edit | »    | \App\Controllers\API\v1\PsikotestAnswersController::edit/$1   | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/answers/(.*)      | »    | \App\Controllers\API\v1\PsikotestAnswersController::show/$1   | is_admin cors  | is_admin cors toolbar |
| POST   | api/v1/psikotests                   | »    | \App\Controllers\API\v1\PsikotestsController::create          | is_admin cors  | is_admin cors toolbar |
| POST   | api/v1/psikotests/answers           | »    | \App\Controllers\API\v1\PsikotestAnswersController::create    | cors           | cors toolbar          |
| PATCH  | api/v1/psikotests/(.*)              | »    | \App\Controllers\API\v1\PsikotestsController::update/$1       | is_admin cors  | is_admin cors toolbar |
| PATCH  | api/v1/psikotests/answers/(.*)      | »    | \App\Controllers\API\v1\PsikotestAnswersController::update/$1 | is_admin cors  | is_admin cors toolbar |
| PUT    | api/v1/psikotests/(.*)              | »    | \App\Controllers\API\v1\PsikotestsController::update/$1       | is_admin cors  | is_admin cors toolbar |
| PUT    | api/v1/psikotests/answers/(.*)      | »    | \App\Controllers\API\v1\PsikotestAnswersController::update/$1 | is_admin cors  | is_admin cors toolbar |
| DELETE | api/v1/psikotests/(.*)              | »    | \App\Controllers\API\v1\PsikotestsController::delete/$1       | is_admin cors  | is_admin cors toolbar |
| DELETE | api/v1/psikotests/answers/(.*)      | »    | \App\Controllers\API\v1\PsikotestAnswersController::delete/$1 | is_admin cors  | is_admin cors toolbar |
+--------+-------------------------------------+------+---------------------------------------------------------------+----------------+-----------------------+
kenjis commented 1 year ago
// Filter : CORS not is_admin
$routes->group('api', static function ($routes) {
    $routes->group('v1', [
        'namespace' => 'App\Controllers\API\v1',
        'filter' => 'cors',
    ], static function ($routes) {
        $routes->resource('psikotests/answers', ['controller' => 'PsikotestAnswersController']);
    });
});

// Filter : is_admin, CORS
$routes->group('api', static function ($routes) {
    $routes->group('v1', [
        'namespace' => 'App\Controllers\API\v1',
        'filter' => ['is_admin', 'cors']
    ], static function ($routes) {
        $routes->resource('psikotests', ['controller' => 'PsikotestsController']);
    });
});
+--------+-------------------------------------+------+---------------------------------------------------------------+----------------+-----------------------+
| Method | Route                               | Name | Handler                                                       | Before Filters | After Filters         |
+--------+-------------------------------------+------+---------------------------------------------------------------+----------------+-----------------------+
| GET    | /                                   | »    | \App\Controllers\Home::index                                  |                | toolbar               |
| GET    | api/v1/psikotests/answers           | »    | \App\Controllers\API\v1\PsikotestAnswersController::index     | cors           | cors toolbar          |
| GET    | api/v1/psikotests/answers/new       | »    | \App\Controllers\API\v1\PsikotestAnswersController::new       | cors           | cors toolbar          |
| GET    | api/v1/psikotests/answers/(.*)/edit | »    | \App\Controllers\API\v1\PsikotestAnswersController::edit/$1   | cors           | cors toolbar          |
| GET    | api/v1/psikotests/answers/(.*)      | »    | \App\Controllers\API\v1\PsikotestAnswersController::show/$1   | cors           | cors toolbar          |
| GET    | api/v1/psikotests                   | »    | \App\Controllers\API\v1\PsikotestsController::index           | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/new               | »    | \App\Controllers\API\v1\PsikotestsController::new             | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/(.*)/edit         | »    | \App\Controllers\API\v1\PsikotestsController::edit/$1         | is_admin cors  | is_admin cors toolbar |
| GET    | api/v1/psikotests/(.*)              | »    | \App\Controllers\API\v1\PsikotestsController::show/$1         | is_admin cors  | is_admin cors toolbar |
| POST   | api/v1/psikotests/answers           | »    | \App\Controllers\API\v1\PsikotestAnswersController::create    | cors           | cors toolbar          |
| POST   | api/v1/psikotests                   | »    | \App\Controllers\API\v1\PsikotestsController::create          | is_admin cors  | is_admin cors toolbar |
| PATCH  | api/v1/psikotests/answers/(.*)      | »    | \App\Controllers\API\v1\PsikotestAnswersController::update/$1 | cors           | cors toolbar          |
| PATCH  | api/v1/psikotests/(.*)              | »    | \App\Controllers\API\v1\PsikotestsController::update/$1       | is_admin cors  | is_admin cors toolbar |
| PUT    | api/v1/psikotests/answers/(.*)      | »    | \App\Controllers\API\v1\PsikotestAnswersController::update/$1 | cors           | cors toolbar          |
| PUT    | api/v1/psikotests/(.*)              | »    | \App\Controllers\API\v1\PsikotestsController::update/$1       | is_admin cors  | is_admin cors toolbar |
| DELETE | api/v1/psikotests/answers/(.*)      | »    | \App\Controllers\API\v1\PsikotestAnswersController::delete/$1 | cors           | cors toolbar          |
| DELETE | api/v1/psikotests/(.*)              | »    | \App\Controllers\API\v1\PsikotestsController::delete/$1       | is_admin cors  | is_admin cors toolbar |
+--------+-------------------------------------+------+---------------------------------------------------------------+----------------+-----------------------+