CodeIgniter Forums
Basic index($variable) question for creating a site like www.mysite.com/username - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Basic index($variable) question for creating a site like www.mysite.com/username (/thread-55839.html)

Pages: 1 2


Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]AlanW[/eluser]
www.mysite.com works fine by directing content to a Main controller by rewriting the URL

Now, I'm trying to get a site looking like twitter, such that my username is passed through as $variable to index

In my default controller, which is Main, I have

class Main extends CI_Controller {

public function index($username=null) {
… code
}

}


The code runs fine only when I eliminate the ($username=null) and just go with index(). With the $username, I get a 404. I've checked the UG but stumped!

Thanks in advance!



Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]siptik[/eluser]
Hi! it doesn't work for you because you call undefined method!
you can change the route.



Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]PhilTem[/eluser]
Show your routes.php config file.


Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]siptik[/eluser]
set route $route['(:any)'] = 'main/index/$1'


Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]AlanW[/eluser]
This is in my route file


$route['default_controller'] = "main";
$route['404_override'] = '';



Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]siptik[/eluser]
try this:
Code:
$route[‘default_controller’] = “main”;
route $route["(:any)"] = ‘main/index/$1’
$route[‘404_override’] = ‘’;



Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]AlanW[/eluser]
[quote author="siptik" date="1352754412"]set route $route['(:any)'] = 'main/index/$1'[/quote]

Is this in addition to the routes I already have?


$route['default_controller'] = "main";
$route['404_override'] = '';



Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]PhilTem[/eluser]
Well yeah, then it's obvious why your code isn't working. Just as @siptik said you need to create a route called

Code:
$route['.+'] = 'main/index/$1';

which will route every request to your page to the main controller's index() method and will pass the uri segments as arguments in order of appearance in the URI.

What your code does is nothing else than allowing URIs like this
Code:
www.example.com/main/index/<username>
which, however, is not what you intentionally wanted Wink


Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]AlanW[/eluser]
Thanks!

[quote author="siptik" date="1352754725"]try this:
Code:
$route[‘default_controller’] = “main”;
route $route["(:any)"] = ‘main/index/$1’
$route[‘404_override’] = ‘’;
[/quote]

Do you mean 'set route $route["(:any)"] = ‘main/index/$1’ …?




Basic index($variable) question for creating a site like www.mysite.com/username - El Forum - 11-12-2012

[eluser]siptik[/eluser]
yes