0

OK, what I need may sound fairly easy (or complicated? - I don't know), but here it is :

In CodeIgniter, given a controller e.g. test you can do :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class test extends CI_Controller {

    public function sub($param1="", $param2="")
    {

    }
}
?>

Which means that you can access either :

  • mysite.com/test/sub
  • mysite.com/test/sub/someparam1
  • mysite.com/test/sub/someparam1/someparam2

However, what happens if I want to "omit" the sub part?

Ok, so I thought about doing the very same thing in the index function of the controller. Like :

public function index($param1="", $param2="")
{
}

So that I could directly access either :

  • mysite.com/test
  • mysite.com/test/someparam1
  • mysite.com/test/someparam1/someparam2

However, given CI's inner design, when I try this, it keeps looking for a someparam1' method in thetest` controller which obviously does not exist.

So, what how would you go about that?


P.S.

1. Please, let's avoid solutions having to do with .htaccess and obscure redirections and let's stick to the most CI-friendly approach available (if there is one).

2. Don't suggest my creating the appropriate functions (instead of using some variable accessor) - if I wanted to do it this way, I would already have done it

3. The parameters should preferably be part of a SEO-friendly URL, and not take them with $_GET, etc. (e.g. mysite.com/test/?param1=someparam1&param2=someparam2)

3 Answers 3

3

try this in config/routes.php

$route['test/(:any)'] = 'test/index/$1';
Sign up to request clarification or add additional context in comments.

Comments

0

You have to access your site like this:

  • mysite.com/test/index
  • mysite.com/test/index/someparam1
  • mysite.com/test/index/someparam1/someparam2

Or you can rewrite in routes.php file

$route['test/(.*)/(.*)'] = 'test/index/$1/$2';

3 Comments

Well, what about eliminating that index part as well? (If the index is there, then I wouldn't care if it was something else, too. Point is to have nothing between the controller name and the potential params...)
then you can do it using my other method in rewrite with routes.php file.
Just tried your additional edit, and - though it definitely looks close - it still doesn't work...
0

You could use some routing configuration:

//if arguments start by a number
$route['test/(^[0-9].+)'] = 'test/index/$1';

//if argument start by a set of choice
$route['test/(str1|str2|str3)(:any)'] = 'test/index/$1$2';
//this can prevent from name collision between methods and first argument

Or, in your controller use magic method:

if any method in that controller not match the URI, this method will be called.

public function __call($name, $args) {
    call_user_func_array(array($this, 'index'), $args );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.