Codeigniter - How to get url variable value inside contoller function?

后端 未结 2 1506
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-11 02:19

I have URL like this: http://localhost/sitename/some-post-title/code=24639204963309423

Now I have one findUser function in my controller file

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-11 03:00

    Are you trying to get a path segment variable or a GET variable? It looks like you're going for a bit of both.

    Natively in CI, you can use $this->input->get if you update your url to look more like

    http://localhost/sitename/some-post-title/?code=24639204963309423
    

    (Note the question mark).

    Alternatively, you can modify your URL to look like this

    http://localhost/sitename/some-post-title/code/24639204963309423
    

    And then use URI segments like so

    $data = $this->uri->uri_to_assoc();
    $code = $data['code'];
    

    If you do not want to change your URL, you will have to break that string up manually like so

    $data = $this->uri->segment(3);
    $data = explode($data, '=');
    $code = $data[1];
    

    I would argue the second option is the most SEO-friendly and pretty solution. But each of these should be functionally identical.

提交回复
热议问题