0

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.

Melloorr
  • 55
  • 5
  • best if you get rid of index.php so that it becomes yoururl/show/1 – TigerTiger May 11 '17 at 12:59
  • Change the href to `show/p/1` and get it using $this->uri->segment('2'). – Ali Zia May 11 '17 at 13:01
  • This documentation has all the answers and great examples. https://www.codeigniter.com/userguide3/general/routing.html You need to enable mod_rewrite module on your server and set-up .htaccess file with awoid index.php, this may help: http://stackoverflow.com/questions/19183311/codeigniter-removing-index-php-from-url – abibock_un May 11 '17 at 13:01
  • `$this->uri->segment(2)` [Docs](https://www.codeigniter.com/user_guide/libraries/uri.html). – Tpojka May 11 '17 at 13:02

2 Answers2

1
  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.
Narf
  • 14,600
  • 3
  • 37
  • 66
0

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.

DFriend
  • 8,869
  • 1
  • 13
  • 26