Welcome Guest, Not a member yet? Register   Sign In
How to setup a single route with optional argument
#5

(This post was last modified: 12-13-2023, 03:39 PM by bgeneto.)

(12-08-2023, 05:25 PM)kenjis Wrote: > the last parameter is optional (the class method has a default value in its second parameter)

There is no such feature now.

PHP Code:
$routes->get('user_details/(:num)''MyClass::userDetails/$1'); 

That's unfortunate. We frequently run across routes that require this last optinal argument. As far as I can recall, this was a feature of the CI v3.

Thanks for your detailed reply! It works indeed... 
But I insist that, according to the docs (at least to my understanding of the documentation), the (:any) placeholder should "match all characters from that point to the end of the URI. This may include multiple URI segments."
And this is not the case, as you can see in this minimal example: 

PHP Code:
$routes->get('/anyplaceholder/(:num)/(:any)''Test::placeholderAny/$1/$2');
$routes->get('/anyplaceholder/(:num)''Test::placeholderAny/$1');

/**
* @see http://localhost:8080/anyplaceholder/1
* @see http://localhost:8080/anyplaceholder/1/path/to/some/folder
*/
public function anyplaceholder($arg1$arg2 null)
{
    return response()->setBody('arg1=' $arg1 ' and arg2=' $arg2);

In the second get request (see method comments) $arg2 will not store the full path passed, it will only contains the string "path".
The same holds true for regex placeholders, from the docs: "With regular expressions, you can also catch a segment containing a forward slash (/), which would usually represent the delimiter between multiple segments."

PHP Code:
/**
* @see http://localhost:8080/regexplaceholder/path/to/some/folder
*/
public function regexPlaceholder($arg1)
{
    return response()->setBody('arg1=' $arg1);

PHP Code:
$routes->get('/regexplaceholder/(.+)''Test::regexPlaceholder/$1'); 

But also in this case only the value "path" will be outputed. At least from me (using CI v4.4.3).
Is this supposed to be the right behaviour? 
Best regards!
Reply


Messages In This Thread
RE: How to setup a single route with optional argument - by bgeneto - 12-13-2023, 03:15 PM



Theme © iAndrew 2016 - Forum software by © MyBB