Where to Start with Yii
So now to add our final feature and that’s to make a menu button on our main page so that we can access our message controller and its new actions. (So we can print our “Hello World”.) You’re probably thinking, oh boy, this is going to be insane, we have to define the link and make it accessible to all the pages and blah blah blah. No. Yii does all this work for you. All you have to do, is change 1 line, actually add 1 line to be more precise. Navigate yourself to the protected/views/layout/ folder and open up the main.php. This is the main display of the template file. It shows the header, the content, the footer, the main menu – everything. It’s basically our html page with template features to just pop in what we need wherever we want to show it. In this case, the template is already set up to pop in our content (from our message controller) all we have to do is point to it. So you need to find:
<?php $this->widget('application.components.MainMenu',array( 'items'=>array( array('label'=>'Home', 'url'=>array('/site/index')), array('label'=>'Contact', 'url'=>array('/site/contact')), array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest), array('label'=>'Logout', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest) ), )); ?>
This is the main menu with all the included buttons from the template. All we have to do is input a new link. Here’s my link, you can customize it to your titles and locations. I put it between the contact and login buttons but feel free to place it anywhere you like within the menu.
array('label'=>'Our Message', 'url'=>array('/message/index')),
And that’s it. Try out your “Hello World” application that implements the Yii controller –model – view concepts. I hope you enjoyed the tutorial and learn a great deal of how Yii functions as well as how to start with building on to the Yii framework. If you’d like to continue to the next tutorial check out my 2nd edition of “Where to Start with Yii – Project 2”.
Some final words of advice, my first programming teacher told us that there were 3 types of programmers in this world: Those that Know, those that Understand and those that are the Fly in the Coke Bottle. Those that know have years of experience with their language and are the professionals in their field, they code with their eyes closed. Those that understand grasp the logic and can build it, code fairly well on their own but research functions and information they need to complete their projects.
So what’s the Fly in the Coke bottle then? Every programmer at some point in their life is or was a fly in the coke bottle. It represents a problem where you are the fly who encounters the issue (fly into the coke bottle) and begin to try a whack of ideas to find something that works as a solution because you don’t know how to find a solution. Basically, you keep buzzing around bouncing on the sides of the bottle until you find the opening again (metaphorically: the solution). You never want to be the Fly in the Coke Bottle and it’s a bad way to program, because it leads to messy and generally bad coding or lack of understand why what you did worked as opposed to why what you were doing didn’t.
However turning those experiences into a programming know why/how, the next time around you’ll know where to start so you can improve the solution. Just remember, every solution can be improved, unless of course it’s echo ”Hellow World”;.
Thanks and happy coding.