Quick Question

Hey guys, new to Yii so just asking some simple questions.

Example problem:

I want to display a random name on a page so that the user is shown "Welcome, [random name]!".

I will have a .txt file with lots of names.

I will have a method that opens and gets a name randomly and returns that as the name.


Where should I put that method?

How do I call it in the index.php?

Put that method in the controller. Then in the view, you can call this method using $this->generateRandomWord().

Thanks :)

Is there a way of calling from another controller?

Personally, I would have a model for the text file.  It would probably directly extend CModel.  The model would have the getRandomWord() function, and possibly others methods too that deal with the file (inserting, deleting, etc).  The model could then easily be used anywhere.

That is what models are for, anyways.  Think of the text file as a storage method, just like MySQL.

Cool, I didn't think of that.

So if I did it that way, would I still want to call from a controller? You can't call from a model can you?

Call what?  You can call model methods from pretty much anywhere - views, controllers, widgets.