Did you know FaceySpacey is the creator of Redux-First Router and Universal?

The Startup Incubator.
Your one stop social media shop
Resource Categories:

Technical
Non-Technical
Refer A Friend
Popular Articles
DEVELOPMENT TOOLS 1 - VERSION CONTROL (Mercurial)
CONTRACTING 2 - TOP 10 EASY SITE CREATOR TOOLS
Subscribe
Yii 2 - CONTROLLERS
In this tutorial you'll learn the path of execution in your code from when a visitor first pings a URL on your site to the HTML delivered to the browser from your controllers and their actions.
Ok, so check it, remember at the beginning of the PHP tutorials you learned how web browsers connect to servers by accessing a script file remotely, and then the server executes the result and serves up the result to your browser, right? Well, in Yii and most MVC Frameworks (“Model View Controller”) only one script is ever pinged. You’re not pinging yoursite.com/script1.php and then yoursite.com/script2.php and then yoursite.com/contact.php, etc. Yii is wired in conjunction with some help of your server (typically Apache, which we’ll cover later, and which ultimately won’t be a big deal in getting productive quick) to make it so all requests to your site go through the same script, called index.php. In the browser, your Apache server will hide index.php from all URLs so you can access your site at yoursite.com without having to access yoursite.com/index.php. Another example would be yoursite.com/about which also would send a request through index.php.
What happens then is that the internals of all the Yii library code route the request to the proper set of Yii code. So if /about is in the URL, Yii will send the request to a code class called “about” and that’s where you write your code. Basically Yii will interepret pretty urls that don’t end with “.php” and route the request based on the words in the complete URL to the appropriate code to execute. capiche.
Code snippet time:
class AboutController extends CController { public function actionUs() { $aboutUsText = “Some stuff about how great your new startup is...”; $this->render(‘viewFile’, array(‘content’=>$aboutUsText)); } }
Now if you were to visit yoursite.com/about/us this code would be triggered. Why? Well obviously because the bolded word “Us” comes after the word “action.” Yii controllers (which as you can see extend from CController) are programmed so that any method that starts with the word “action” correspond to specific pages requested. So the idea is when your request starts with “/about” then the AboutController is called, and specifically within it if after “/about” is “/us” like this “/about/us”, it’s “action method” that ends in “Us” is executed.
Now, to get a page to render to the browser, you have to call the “render()” method of the current controller class, which as you remember will require using the “$this” keyword. Yii controllers have a method called “render” which is all about displaying a specific view file, in this case called “viewFile” which is bolded to point out its importance. That view file (which is a php code file) is then displayed in the browser. That’s it.
Lastly, the view file (which will be named somewhere as “viewFile.php”) will be passed a variable called $content with the value of $aboutUsText. So in the view file you can place the $content variable anywhere you want to display the paragraph about your startup. The view file will be a combination of HTML and PHP snippets of dynamic code interspersed within it.
Comments

SMM 3 - FORMULA TO FIND INFLUENCERS