Welcome Guest, Not a member yet? Register   Sign In
Please explain the router code.
#1

I can't think of a case where this code would make sense.

PHP Code:
$replacekey str_replace('/(.*)'''$routeKey);
$handler    preg_replace('#^' $routeKey '$#u'$handler$uri);
$handler    str_replace($replacekeystr_replace('/''\\'$replacekey), $handler); 

PHP Code:
// for route 
$routes->get('path/(:segment)/(:any)''Controller::method/$1/$2');

// $routeKey = path/([^/]+)/(.*)
$replacekey str_replace('/(.*)'''$routeKey); // path/([^/]+)

$handler    preg_replace('#^' $routeKey '$#u'$handler$uri);  // \App\Controllers\Controller::method/1/2

$handler    str_replace($replacekeystr_replace('/''\\'$replacekey), $handler); // replacing "path/([^/]+)" with "path\([^\]+)" in "\App\Controllers\Controller::method/1/2" 
Reply
#2

(This post was last modified: 05-16-2022, 08:43 PM by kenjis.)

The last `str_replace()` is not covered by PHPUnit tests.
RouterTest passes if I remove the line.

PHP Code:
$routeKeyapi/posts/(.*)
$replacekeyapi/posts
$handler
: \App\Controllers\Api\PostController::show/50
$handler
: \App\Controllers\Api\PostController::show/50 

PHP Code:
$routeKeyapi/posts/(.*)/edit
$replacekey
api/posts/edit
$handler
: \App\Controllers\Api\PostController::edit/50
$handler
: \App\Controllers\Api\PostController::edit/50 

PHP Code:
$routeKeyapi/posts/(.*)
$replacekeyapi/posts
$handler
: \App\Controllers\Api\PostController::update/50
$handler
: \App\Controllers\Api\PostController::update/50 

It seems the code came from https://github.com/codeigniter4/CodeIgniter4/pull/344
Reply
#3

(This post was last modified: 05-16-2022, 08:56 PM by iRedds.)

Perhaps some separate logic was planned here.
But I don't see the point in the first and second line.

That is, the condition can be reduced to a single preg_replace() call.
Reply
#4

(This post was last modified: 05-16-2022, 09:11 PM by kenjis.)

From the time of the first PR, it looks the test passes without it.

Code:
--- a/system/Router/Router.php
+++ b/system/Router/Router.php
@@ -398,9 +398,9 @@ class Router implements RouterInterface
                    // ex: $routes->resource('Admin/Admins');
                    if (strpos($val, '$') !== false && strpos($key, '(') !== false && strpos($key, '/') !== false)
                    {
-                      $replacekey = str_replace('/(.*)', '', $key);
+//                    $replacekey = str_replace('/(.*)', '', $key);
                        $val = preg_replace('#^'.$key.'$#', $val, $uri);
-                      $val = str_replace($replacekey, str_replace("/", "\\",$replacekey), $val);
+//                    $val = str_replace($replacekey, str_replace("/", "\\",$replacekey), $val);
                    }
                    elseif (strpos($val, '$') !== false && strpos($key, '(') !== false)
                    {

(05-16-2022, 08:55 PM)iRedds Wrote: Perhaps some separate logic was planned here.
But I don't see the point in the first and second line.

That is, the condition can be reduced to a single preg_replace() call.

Agreed.
We can remove the if statement.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB