CodeIgniter Forums
How to setup a single route with optional argument - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to setup a single route with optional argument (/showthread.php?tid=88954)

Pages: 1 2


RE: How to setup a single route with optional argument - bgeneto - 12-14-2023

(12-14-2023, 04:02 AM)kenjis Wrote: Out of curiosity, why don't you send a pull request to update the docs?

Only because I never remember where the documentation source code is. So I will post it here for reference (for me and others) : 

https://github.com/codeigniter4/CodeIgniter4/tree/develop/user_guide_src/source

Regards.


RE: How to setup a single route with optional argument - kenjis - 12-14-2023

(12-14-2023, 03:47 AM)bgeneto Wrote:
Quote:With regular expressions, you can also catch a segment containing a forward slash (/), which would usually represent the delimiter between multiple segments.

For example, if a user accesses a password protected area of your web application and you wish to be able to redirect them back to the same page after they log in, you may find this example useful:

PHP Code:
<?php

$routes
->get('login/(.+)''Auth::login/$1'); 

But that's not the case, unless you rely on variable-length argument lists, i.e., multiple arguments. 

I have re-read the user guide and this description and the current behavior still do not match.
It is natural that the string that matches (.+) would be the one param $1.

If we define a route like this:
PHP Code:
$routes->get('products/([a-z]+)/(.+)''Products::show/$1/id_$2'); 

and navigate to the URI products/shirts/123/456, the controller method parameters would be like:
Code:
$params array (3)
    ⇄0 => string (6) "shirts"
    ⇄1 => string (6) "id_123"
    ⇄2 => string (3) "456"

It seems to me that the current behavior should be changed.


RE: How to setup a single route with optional argument - bgeneto - 12-15-2023

(12-14-2023, 04:47 PM)kenjis Wrote: I have re-read the user guide and this description and the current behavior still do not match.
It is natural that the string that matches (.+) would be the one param $1.

Thank God :-) That's the whole point of this thread. For me the docs description does not match the behavior of (.+) and (:any). 
(:any) will NOT match all characters from that point to the end of the URI. It will match all characters till the first forward slash(/).

(12-14-2023, 04:47 PM)kenjis Wrote: It seems to me that the current behavior should be changed.

Totally!


RE: How to setup a single route with optional argument - kenjis - 12-16-2023

I found the current behavior came from CI3.

Code:
$route['login/(.+)'] = 'auth/login/$1';

In the above example, if the $1 placeholder contains a slash, it will still be split into multiple parameters when passed to Auth::login().
https://www.codeigniter.com/userguide3/general/routing.html#regular-expressions

So it seems difficult to fix it as a bug.

First, I sent a PR to improve the documentation.
https://github.com/codeigniter4/CodeIgniter4/pull/8340

And I will try to add an option to pass multiple URI segments as one parameter.


RE: How to setup a single route with optional argument - Kaosweaver - 12-19-2023

(12-15-2023, 05:22 AM)bgeneto Wrote:
(12-14-2023, 04:47 PM)kenjis Wrote: I have re-read the user guide and this description and the current behavior still do not match.
It is natural that the string that matches (.+) would be the one param $1.

Thank God :-) That's the whole point of this thread. For me the docs description does not match the behavior of (.+) and (:any). 
(:any) will NOT match all characters from that point to the end of the URI. It will match all characters till the first forward slash(/).

(12-14-2023, 04:47 PM)kenjis Wrote: It seems to me that the current behavior should be changed.
Totally!
Thanks! I'm in the midst of routing manually the entire site for an upgrade and I thought I was really messing up. Now I know it isn't me.


RE: How to setup a single route with optional argument - kenjis - 12-19-2023

(12-16-2023, 05:30 PM)kenjis Wrote: And I will try to add an option to pass multiple URI segments as one parameter.

https://github.com/codeigniter4/CodeIgniter4/pull/8348