CodeIgniter Forums
First timer ... how to pass variable on default controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: First timer ... how to pass variable on default controller (/showthread.php?tid=23612)



First timer ... how to pass variable on default controller - El Forum - 10-16-2009

[eluser]sdotsen[/eluser]
I've been following the 20 minute blog tutorial on CI's homepage.
I understand how it's done when you're using one controller but what about multiple controller?
Let me explain.

http://localhost/site (my default controller - my home page if you will)
http://localhost/profile/sue (I want to display Sue's profile)

My site view contains the following code:

Code:
<?php while ($item = $records->getNext()): ?>
            <?=anchor('profile/'.$item["username"], $item["username"]);?>
            <?=$item["age"];?>
            <br>
    &lt;?php endwhile; ?&gt;

This will generate ...

Sue 28
Bob 20
Gary 30

The link shows http://localhost/profile/sue but when I click on it, it freaks out saying no page found.
I remove the username and of course the page loads. It appears that CI is expecting to find a function going by the username. How can I fix this?


First timer ... how to pass variable on default controller - El Forum - 10-16-2009

[eluser]sdotsen[/eluser]
Nevermind, I had to set some routes.

$route['profile'] = "profile/index";
$route['profile/(:any)'] = "profile/index/$1";


First timer ... how to pass variable on default controller - El Forum - 10-16-2009

[eluser]jedd[/eluser]
Hi sdotsen,

From the URL's you've given there, your application is expecting to find a controller called profile and another one called site - try creating that second controller, with just a simple echo in it.

If you are heading to a URL such as /profile/sue - it means that your controller profile is looking for a method or function in there called sue.

Rather than trying to re-route things, which can be a bit confusing when you're learning, try thinking about what you're doing with the profile - in this case you want to view it. So if you think of controller names are nouns, and controller methods as verbs, you'd head towards a structure thus:
controller : profile
method : view
parameters of view : ( $username )

Does that make sense?


First timer ... how to pass variable on default controller - El Forum - 10-16-2009

[eluser]sdotsen[/eluser]
Hi Jedd ...

Makes complete sense and yes I have a second controller and I was able to see my "Hello World" when I visit http://localhost/profile

I understand it was looking for a function called sue. I searched around and realize I needed a "route." If there are other ways of dealing with it, I'm open to advice. I used to deal w/ htaccess for these kind of issues and I hated it!!!!

As for creating a new function call "view," I would like to keep my URL as short as possible. I think I tried that and yes it did work as you suggested.


First timer ... how to pass variable on default controller - El Forum - 10-16-2009

[eluser]jedd[/eluser]
My advice stands - stick with the noun/verb metaphor for controllers/methods, and consequently adjust your URLs here.

Routes are often used as a quick and dirty hack to resolve profound, yet very simple design problems. Try to think of your problem, and your design, in ways that avoid requiring routes.