Where to Start with Yii
Rate Topic:

[UPDATE Oct 11/09]
In a new comprehensive guide I'm working on, I remodelled this tutorial to fix any bugs, make it work without the need for a database and provided screen shots. Here is the link, it's in PDF format.

Hi folks. I'm new to Yii, I'm new to frameworks so in the process of trying to find out myself where to start I designed this tutorial. For most of the users who submit to these parts of the forums this will be completely useless to you except to critique. However this is meant to be geared at complete new users to Yii, even new to PHP Frameworks. Maybe if it's good enough it'll be something they start using to introduce users to Yii.

I would love for people to have a read and rate it on it's simplicity, clarity and anything else you want to comment on. I will warn you it is roughly 5000 words (approx 10 typed pages) in length so it is timely to go through. However even if you just skim through the parts that are important to the "coding" as opposed to the defining MVC and OOP, it may be shorter.

There is also a lot of "dialogue" if you wish about my own personal views and exp​ressions that I find help personalize the tutorial and make it more user friendly giving the reader a better sense of what they are doing.

I can make revisions as needed for typos, clarity or generally just adding / removing parts of it. It's written in Canadian English. Enjoy

[New Edit] Apparently the new forums altered the links, heres' the address.

www.sterlingsavvy.com/tutorials/index.php

Also would like to add a little note: just established that in order for this to work, a db connection has to be setup. To bypass that issue, just create your own model class (since that's the problem because of CActiveRecord). I will be making a new updated version soon in a total Yii guide.

Here are my comments:
1. The model and crud commands in yiic shell will not work without a DB connection (the model command actually works in 1.0.5, but crud requires 'db'). In the upcoming 1.0.6, both would require a 'db' connection. Since you are showing working with a form model, maybe you may manually create the needed files. That would also cause less confusion to beginners.

2. In actionIndex(), why would you check isset($message->message)? Since $message is new, this check would always fail unless you set some default message value.

3. In the index.php view file, the links can be created using syntax like the following, which avoids embedding direct URLs in your code:

Thanks for the comments. Yeah I can look to make it a more user friendly HTML version and even break it up into sections of the process. I'll work on something and post a link to it.

As for the yiic commands, yes I realize building a model implies you're using a db (hence the extends CActiveRecords of the class) and a table name is usually the model name. However because I didn't know what I was doing with Yii, I was writing the tutorial as I went along and used that as my building bases for the controller. Now I understand that the models go under the model directory and the associated views are made within a folder called YourID for the views. Stuff that if you start from scratch just building your own files you'd have to already know the architecture really well.

I think having the pre-built templates help to new users just to play with things and see what they do. It's easier for beginners to work with a class already built then to have to start one from scratch, it takes a great understanding of both the framework and OOP to do that. In my second tutorial I was going to work with that, creating the classes from scratch - teaching users how to read from the Yii class reference documentation to find the methods that help them complete what they want to do.

As for actionIndex(), I'll be completely honest I was thinking of originally making it save variable information so that way the edited message gets displayed when you view the show action. However I soon realized that A) I'm not working with a db, so it's not stored there anywhere. And I'm not working with sessions (haven't looked into it so I'm not to sure how they work with the framework).

Thus isset($message->message) is pretty useless except it did it's toll for the editing action, because the edit action forwards to the show action when you edit the string. When that happens, that isset() actually returns true and it renders the newly editing message, it just doesn't keep it. I didn't bother being picky about it because it was just something simple that was only emphasizing how to use models with controllers and views.

If there's a better way to evaluate the process I'm inclined to hear it, after all it's a learning process for me too .

Finally thanks for pointing out that about the CHtml::link, I'll change them to that format. I don't know why I didn't catch it myself, I mean I used it in the main menu lol. I'll actually make a reference to it as well in the tutorial because a link is probably one of the more widely used html actions beginners will try to use.

Thanks again for the comments, I'll post a message when everything is updated.

So I built a template setup from scratch, nothing fancy, for the html version. I must say that the html <code> and <pre> tags are a pain in the ass to use, but the fact that I did everything in php using echo statements probably didn't help.

I haven't gotten around to doing screen shots yet however I do like the idea and will do some, it's just because the project is already completed on my part I have to do it again and take screens as I go (instead of modifying all the required code).

This will give me a chance to follow my own tutorial and see how it reads and what to do. I'll probably add in a thing or two that isn't clear (like the public variables for the model).

Anyway here's the link: http://www.sterlings...rials/index.php , all comments are welcomed. I also updated all the links to the proper Yii CHtml use (much easier and nicer I might add lol) and I made some note references about the isset action for the show page and why it won't log the variables (thus always fail) unless coming from the edit page.

As for the idea of manually creating the model / controllers due to using form instead of AR, I think I'll leave it as that because it lets the users play around with it and also if they want to go above and beyond and use a db to hold the string message information, they can. More importantly it's simplier than trying to build a class from scratch which I will be doing for the 2nd tutorial which I will do and post there. I have a feeling I'll be doing quite a few tutorials on here, maybe 1 a day if I can muster it (maybe not design wise but write ups for sure) because I'm still learning Yii myself, but I'm so impatient! I just want to build my site lol.

[EDIT]

Figured I'd just mention that after a little more review of the tutorial from feedback and my own views, I'll proceed to place it in the cookbook. I just want to make sure it's as clear and easy to follow as possible before I do that.

Your page layout has problem because the main section goes below the TOC. Other than this, it looks good to me. Putting it in cookbook is a good idea. You may separate it into many cookbook pages and have a TOC page to link them together.

Gah, did I ever mention how much I hate css coding for cross-browser capability lol. IE doesn't put a vertical slider for some reason, but you can select the text and and drag down? That's just messed. I'll get on that when I get the chance. thanks for pointing it out, often when I do small things I just check good old FF to see how things work but for get to check other browsers.

I haven't done anything with this in a while. As soon as I finish up my Ajax readings and ActionScript I'll be back to Yii and I'll be making a full guide from the ground up with a decent site setup and set it through in the cook book section. Eitherway, this should introduce you to how Yii works with models, views and controllers and give you a little bit on OOP and MVC framework design.

why I dont understand you say?
I dont know how to load and dont know to start the yii.
I'm studying in Thaiload. This year I am doing a project finished.

I hope someone will advise me.

thak

Send me a pm of what you know and don't know and where you're at and I'll see about getting you started 1 on 1. This tutorial is a little jumbled (my fault) as it's using some methods that may not work if you do not have an active database connection.