Wednesday, 17 April 2024

Query String URI in Route in Code codeigniter 4

 


You can pass some information using the query string. Query string is very useful for creating dynamic page. As you can see in the image given above, the Query string is a circle of red color. Page content will be changed as per the product ID provided. 

Step 1 


Goto App>confirg>Routes.php   add the Routes rule

$routes->get('product/(:any)', 'Home::product/$1');

In this write product specified the Page URL  and (:any) specified, any thing after product/ will be consider query string . Which is get by the Home Controller's product method . $1 denote only one argument will be pass.

Step 2 (Controller code)



  public function product($arg)
    {
        $data['title']='Contact Us';
        $data['arg']=$arg;
        return view('product',$data);
    }

query string will be save in $arg variable and this variable will pass on view by $data['arg']


Step 3 (View)


You can access query string on view by using $arg variable.

echo $arg;


No comments:

Post a Comment