Codeigniter. How to pass parameters $ _GET in the

2019-09-20 07:50发布

问题:

This code $route['basketball'] = "controller/product/?id=7" does not work.

function product()
{
    echo $_GET['id']  // no output
}

How to describe the rules in the route?

回答1:

If possible use CodeIgniter's standard URL routes. In your case:

$route['basketball'] = "controller/product/7";

function product()
{

}

OR if $_GET['id'] needs to be dynamic

$route['basketball/:num'] = "controller/product";

function product($id)
{

}

Hope that helps.



回答2:

Because you are in PHP , you can basically set $_GET and $_REQUEST parameters which are super global variables that can be accessed anywhere in the code. So you can do a callback and set them there.

For example:

$route['basketball'] = function(){
  $_GET['id']=$_REQUEST['id'] = 7;
  return "controller/product/";

};

Then in your code you can access $_GET['id'] or whatever.