EllisLab text mark
Advanced Search
1 of 2
1
   
Basic index($variable) question for creating a site like www.mysite.com/username
Posted: 12 November 2012 03:31 PM
Joined: 2012-10-08
15 posts

http://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!

 
Posted: 12 November 2012 03:56 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-06
49 posts

Hi! it doesn’t work for you because you call undefined method!
you can change the route.

 
Posted: 12 November 2012 04:03 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

Show your routes.php config file.

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 12 November 2012 04:06 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-06
49 posts

set route $route[’(:any)’] = ‘main/index/$1’

 
Posted: 12 November 2012 04:09 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-10-08
15 posts

This is in my route file


$route[‘default_controller’] = “main”;
$route[‘404_override’] = ‘’;

 
Posted: 12 November 2012 04:12 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-06
49 posts

try this:

$route[‘default_controller’] “main”;
route $route["(:any)"‘main/index/$1’
$route[‘404_override’] 
‘’

 

 
Posted: 12 November 2012 04:12 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-10-08
15 posts
siptik - 12 November 2012 04:06 PM

set route $route[’(:any)’] = ‘main/index/$1’

Is this in addition to the routes I already have?


$route[‘default_controller’] = “main”;
$route[‘404_override’] = ‘’;

 

 
Posted: 12 November 2012 04:12 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

Well yeah, then it’s obvious why your code isn’t working. Just as @siptik said you need to create a route called

$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

www.example.com/main/index/<username

which, however, is not what you intentionally wanted wink

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 12 November 2012 04:14 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2012-10-08
15 posts

Thanks!

siptik - 12 November 2012 04:12 PM

try this:

$route[‘default_controller’] “main”;
route $route["(:any)"‘main/index/$1’
$route[‘404_override’] 
‘’

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

 

 
Posted: 12 November 2012 04:17 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-06
49 posts

yes

 
Posted: 12 November 2012 04:20 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2012-10-08
15 posts
AlanW - 12 November 2012 04:14 PM

Thanks!

siptik - 12 November 2012 04:12 PM

try this:

$route[‘default_controller’] “main”;
route $route["(:any)"‘main/index/$1’
$route[‘404_override’] 
‘’

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

 

Actually, I wondered if you need the word ‘set’ … or route.

It looks like the other lines start with $route so I’ll try adding:

$route[”(:any)”] = ‘main/index/$1’; 

 

 
Posted: 12 November 2012 04:26 PM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-06
49 posts

I’m sorry that I unclear wrote

 
Posted: 12 November 2012 04:29 PM   [ # 12 ]   [ Rating: 0 ]
Joined: 2012-10-08
15 posts

Thank you all!

siptik - 12 November 2012 04:12 PM

try this:

$route[‘default_controller’] “main”;
route $route["(:any)"‘main/index/$1’
$route[‘404_override’] 
‘’

I added this:

$route[’(:any)’] = “main/index/$1”;

to my routes config file.

The http://www.mysite.com/username is now recognized by

public function index ($username){
}

BUT my other controllers such as http://www.mysite.com/notitications (controller is notifications.php ) are now not functioning.  I finally get it - the site now thinks that there is only one page - not my plan.  Is there some way to again recognize the the other controllers?

 

 
Posted: 12 November 2012 04:31 PM   [ # 13 ]   [ Rating: 0 ]
Joined: 2012-10-08
15 posts
siptik - 12 November 2012 04:26 PM

I’m sorry that I unclear wrote

No problem - I really appreciate the help!

 
Posted: 12 November 2012 04:41 PM   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-06
49 posts

you need to add route for each controllers

 
Posted: 12 November 2012 05:38 PM   [ # 15 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

Maybe you want to have a look at this kinda tutorial I wrote some months ago for other people having the same problem:

http://files.ignitedco.de/codeigniter/tutorials/static-pages-routing.html

It does not match your problem perfectly, but you might understand how the added route works.

BTW: Every route that you add must go after the default_controller and 404_override route. And watch out to take the correct apostrophes - PHP uses quotation marks and no apostrophes wink

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
1 of 2
1