Welcome Guest, Not a member yet? Register   Sign In
Basic index($variable) question for creating a site like www.mysite.com/username
#1

[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!
#2

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

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

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

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


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

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

[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'] = '';
#8

[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
#9

[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’ …?

#10

[eluser]siptik[/eluser]
yes




Theme © iAndrew 2016 - Forum software by © MyBB