使用Codeigniter使用segment和URI获取变量[关闭]

Okay,

So I am looking to do something similar to index.php/show?p=1, however I know that using GET is disabled in Codeigniter by default. So I was wondering how I would set this up in a controller using URI and Segments, so it would look like index.php/show/1. Or is this not possible?

Obviously the '1' will be changed based on a value in the database.

I hope this makes sense, if not, please let me know.

  1. $_GET is not disabled by default.
  2. Doing the URI segments thing is as easy as adding a single route, and this is covered by the tutorial in the official documentation.

To have the URL http://example.com/index.php/show/1 do as you ask you would modify the file application/config/routes.php to accomplish the task.

Assuming show is a controller and you want the index() method of the controller to handle the above URL you need to do a couple things.

Define the index method to accept an argument

public function index($arg) 
{
    ...

Add the following to application/config/routes.php

$route['show/(:any)'] = 'show/index/$1';

If you want to use some method other than index() to handle the chore - let's say doShow() then the route looks like this.

$route['show/(:any)'] = 'show/doShow/$1';

And the method is

public function doShow($arg) 
{ 
   ...

No matter what method used - index() or doShow() - if you don't pass an argument in the second URI segment you will get a 404 - Page Not Found screen.