How Does A Controller In Yii Work?

Thanks For Sharing! :-)Share on Google+Tweet about this on TwitterShare on FacebookShare on RedditBuffer this page

How Does A Controller In Yii Work

As we dig in to the Yii Framework further, it seems like an in depth examination of controllers makes a good next step. In this episode we will take a closer look at the generated controller from our advanced installation. Since the code here is a result of what the Yii developers created for the starter template, we can be sure it is created in a best practice sort of way. Before we start modifying and changing any code to create our own custom applications, we need to understand how the base code working. By analyzing each of the sections of the base SiteController.php with links to the associating documentation, we’ll learn how things are working. Consider it a cheat sheet of sorts for working with controllers in Yii.

PHP Short Array Syntax

One thing to brush up on is your array syntax, specifically the shorthand form of arrays introduced in php5.4. In there is are two things you will see all the time in Yii, it is configurations, and arrays with short syntax. Almost everything in Yii is configured via an array in the shorthand bracket form.

Arrays can now be simply defined via square brackets.

instead of

While we’re on the topic of arrays, it’s worth mentioning that they now also support dereferencing, meaning you no longer need to assign the array to a temporary variable to access it.

Now you can simply do this:

PHP Namespaces

Namespaces are here, and they are in use across most modern codebases. Yii is no different, and makes use of namespaces as well. If you need to, you can brush up on namespaces at the official docs.

http://php.net/manual/en/language.namespaces.php

The Yii Controller

When creating a controller in Yii, you extend the Controller class.
http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html#controllers

You now have a controller ready for use in Yii. We’re going to approach the way we review the controller based on the number of times various method calls happen in the generated code. First up, we have the render() method which is called 7 times in the provided SiteController. Let’s see what this method does.

render()

http://www.yiiframework.com/doc-2.0/yii-base-controller.html#render%28%29-detail

The render method takes the name of the view to render as a string for the first parameter, and an array of key value pairs for the second parameter. Just like most MVC frameworks, the second parameter becomes the variables that hold the data to be accessed in the view. Note that that these values are not available in the layout file. A final tidbit to remember is that it is by the act of returning the render method that the output is actually generated. This is why calls to render look like this

Redirection

Next up we have some utility type methods that get used a fair amount. To redirect a user to the home page, you can use return $this->goHome(). To send a user to the page they were just visiting you would use return $this->goBack() and to refresh the page you can use return $this->refresh(), all quite handy and useful.

Flash Messages

There are several instances in the advanced application where flash messages are put to use. Flash messages are convenient messages that get displayed to the user for only one page load, they then automatically expire. We find these lines in the SiteController.php to provide these messages.

An interesting thing to note is that some of the messages use the approach of $app->getSession()->setFlash() while others are using $app->session->setFlash(). It looks like two ways to do the same exact thing. Is one better than the other? I have no idea, test them out and see what you like best. This is the first part of using flash messages in Yii, which is to say you need to set the flash type, and flash message in the controller. You can see how that is done just above, i.e., setFlash() takes two parameters with the first being the type, and the second being the actual message. Now by practice, you typically fetch and render flash messages in your layout file. What’s really amazing is that all you have to do is simply have the code

present in your view file (or more commonly the main layout file) and your flash messages just simply work. No need to check for a flash message or anything like that. The alert widget is intelligent enough to simply display, remove, and otherwise manage alerts based on whether you called setFlash() in your controllers. That’s a really nice feature.

Calling on Models

Throughout this controller we can see that many new instances of models get created. Here are the ones we found.

One note to remember is that it looks like the convention that is used here is that when new models are created, they get assigned to $model, no matter what type of model they actually are. Therefore, you need to pay attention to where you are to be sure you are dealing with the right object instance. This is probably quite obvious, but it’s something I noticed and figured it was worth mentioning. So what are the common uses of these model instances? Well, this scenario looks like it happens a bunch.

If you are used to creating html forms by hand, then using native PHP to manage the submission process, you’ll know there are a bunch of steps to keep track of. Did the user provide data? Did they provide the right type of data? Did they submit the form? Did they try to hack the form? Is the data they submitted valid? The list goes on. With the simple line above, you pretty much take care of all those things in one shot. As long as you set up your models and forms correctly, like we did in the active form tutorial, things will just work. The slightly confusing thing with this is that the framework is certainly a “higher level” approach. It’s as if you’re pushing various buttons, and having faith that things will get taken care of. Or better yet, we can think of it like macros, or a set of related commands that get triggered when needed. The simple way of saying what the code above does is, “Did the user submit the form and is it valid?” The reality is that there are many more steps and logic involved than just this simple explanation.

Conclusion

Controllers in Yii are pretty straightforward, as they work like most other controllers might in a Model View Controller framework. In this episode we looked at some of the common methods that get used in the advanced template created when we first install Yii. It looks like this is a great way to learn Yii, i.e., build out small applications using the provided generation tools and then reverse engineer so to speak how those lines of code are actually working. As we get more comfortable with the framework, we start to see patterns, similarities of approach, and an overall sense of Zen.

Thanks For Sharing! :-)Share on Google+Tweet about this on TwitterShare on FacebookShare on RedditBuffer this page

How To Generate Twitter Bootstrap Markup With Yii 2

Thanks For Sharing! :-)Share on Google+Tweet about this on TwitterShare on FacebookShare on RedditBuffer this page

How To Generate Twitter Bootstrap Markup With Yii 2

Another great feature of the Yii 2 Framework is that it supports Twitter Bootstrap right out of the box. You can use the familiar Yii syntax to generate this markup in your views using Widgets. Widgets in Yii are like little reusable chunks of code that output data in a nice formatted way. Like most things in Yii, they work by loading in a configuration in the form of an array. If you’re not using the short array syntax yet in PHP, you certainly will be once you start using Yii 2 since it makes liberal use of the short array syntax, as in everything is in this form [] instead of this $hi = array();. In any event, let’s look at some of the ways to use the built in Widgets for generating Twitter Bootstrap Markup in Yii2.

Carousel yii\bootstrap\Carousel


Tabs yii\bootstrap\Tabs

The Yii 2 Framework is a Fast, Secure and Awesome PHP Framework you need to check out.

jQuery is the worlds most popular JavaScript Framework that lets you easily work with the DOM.

Twitter Bootstrap is a super HTML, CSS, and JavaScript Framework to make building your front end a snap.

You can add as many tabs as you like. Just populate the array as needed to add more tabs to the UI.

Time to Rock Out With Your Socks Out with Yii 2 and Twitter Bootstrap. All baked into one nice package.

One is the lonliest number, but two is company. Two is one plus one which equals two.

Three has been known to be a Crowd. Three is odd, not even. If you like three, the trifecta is yours.


Alert yii\bootstrap\Alert

Hey Buddy, Check out this cool information.

You Win! Great work with your success.

Hold Up Friend, Read this important information before proceeding.

Stop! Something is about to break.


Buttons yii\bootstrap\Button


ButtonDropdown yii\bootstrap\ButtonDropdown


Collapse yii\bootstrap\Collapse

This is the content area. Think of something cool to say.
Peek a boo, I see you. I was hiding in the content area.
I bet you didn’t know that was coming :-)


Modal yii\bootstrap\Modal


Nav yii\bootstrap\Nav

Navbar yii\bootstrap\Navbar


Progress yii\bootstrap\Progress

Awesome60% Complete
65% Complete
70% Complete
70% Complete
30% Complete
Super Awesome30% Complete
35% Complete


ActiveForm yii\bootstrap\ActiveForm




Yii 2 Bootstrap Tips

Now that we’ve looked at many of the live examples that Yii 2 offers for Twitter Bootstrap right out of the box, let’s talk about a few tips that may help you.

1. Don’t Forget Your Use Statements

In your excitement to get rolling with creating awesome widgets in your views, don’t forget to add the proper use statements to the top of the file. To use everything covered in this episode, simply put this code at the top of the view file in question:

Once this is in place, you’ll be able to make calls to the various widget methods successfully.

2. Use A Code Editor That Has Built In Code Formatting

There are plenty of different code editors that will auto format your code for you. Why is this helpful? This is helpful because you will go batty trying to keep track of what arrays go where. As you work more with Yii, you’ll begin to realize that most things are created with config files by way of an array. That array may contain other arrays which contain other arrays and so on. It can get a little tricky if you don’t have syntax formatting to help visualize how everything is contained within the main configuration array.

3. How To Enable The Bootstrap Theme

In Bootstrap 3, the base style is flat. It looks nice, but if you’d like to turn on the theme to have more gradients and shadow effects, you can update the AppAsset file like so.

assets/AppAsset.php source

Conclusion

In this episode, we learned all about creating Twitter Bootstrap UI Elements by programming them in Yii 2. It’s a neat way to generate your front end UI by way of creating the back end code in Yii to complete this for you.

Thanks For Sharing! :-)Share on Google+Tweet about this on TwitterShare on FacebookShare on RedditBuffer this page