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

(This post was last modified: 12-08-2023, 11:45 PM by ozornick.)

Create 2 routes: 

user_details/(:num)/(:any)
user_details/(:num)

for one controller MyClass->userDetails($a, $b=null) ?

EDIT: Its work fine


PHP Code:
// Routes
$routes->get('/products/(:num)/(:any)''Product::oneSegment/$1/$2');
$routes->get('/products/(:num)''Product::oneSegment/$1');

// Controller
class Product extends BaseController
{
    /**
    * @see http://localhost:8080/products/111
    * @see http://localhost:8080/products/111/adaw/adawd/awdawdeg/
    */
    public function oneSegment($segment1, ...$segments)
    {
        return response()->setBody('Product::oneSegment with ' $segment1 ' and ' implode(', '$segments) . ' loaded');
    }
}

// Output
Product::oneSegment with 111 and loaded
Product
::oneSegment with 111 and awdawadawd loaded 
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply


Messages In This Thread
RE: How to setup a single route with optional argument - by ozornick - 12-08-2023, 10:49 PM



Theme © iAndrew 2016 - Forum software by © MyBB