separate header, content and footer in yii - yii

I have a login form in my header section of the website. If user is logged in than insted of the login form user profile details will be shown. The question is how to separate header footer and content into different views and call them from one controller? Or maybe there is another solution...Thanks for help.

In your header view you could write something like this.
<?php if(Yii::app()->user->getId()): ?>
<?php $this->renderPartial('//world/_header_user')); ?>
<?php else: ?>
<?php $this->renderPartial('//world/_header_guest')); ?>
<?php endif; ?>

Using the Model-View-Controller (MVC) design pattern, the look of a Yii-based site is naturally controlled by the View files. These files are a combination of HTML and PHP that help to create the desired output. Specific pages in a site will use specific View files. In fact, the View files are designed to be broken down quite atomically, such that, for example, the form used to both create and edit an employee record is its own file, and that file can be included by both create.php and update.php. As with most things in OOP, implementing atomic, decoupled functionality goes a long way towards improving reusability. But the individual View files are only part of the equation for rendering a Web page. Individual view files get rendered within a layout file. And although I’ve mentioned layouts a time or two in my writings on Yii, it’s a subject that deserves its own post.
To be clear, layouts are a type of View file. Specifically, whereas other View files get placed within a directory for the corresponding Controller (i.e., the SiteController pulls from views/site), layout files go within views/layouts. But while the other View files are associated with individual Controllers (and therefore, individual pages), layouts are communal, shared by all the pages. Simply put, a layout file is the parent wrapper for the entire site’s templating system. I’ll explain
ypu can see more details
http://www.larryullman.com/2012/05/16/working-with-layouts-in-yii

The easiest way is probably to use a different layout, which you just switch on login. If not, showing partials / components based on Yii::app()->user->isGuest also works well.

Your default generated Yii application has a parent Controller in protected/components/Controller.php.
If you need to access additional parameters in layout, add public properties to Controller, set them in your child controller, and use them in your view/layout files.

Related

How to insert custom inline javascript code in php phalcon framework

I want to insert from Controller in the end of some view's code like this
"<"script> setInterval(function(){ alert("Hello"); }, 3000);"<"/script>
How to do this?
There's multiple ways to do this in PhalconPHP. Depending on your needs, you might be able to get away with just:
$js='setInterval(function(){alert("Hello");},3000);';
$this->view->js=$js;
then from the view, you'd do:
<script><?php echo $js; ?></script>
If you need to insert the JavaScript into your top-level index.phtml from a nested view, the way to accomplish this is to first edit your ControllerBase.php and define a new collection:
$this->assets->collection('footer');
Then add a reference to the footer collection from your top-level index.phtml file:
<body>
<?php echo $this->getContent(); ?>
<?php $this->assets->outputJs('footer'); ?>
</body>
You could also use output('footer') instead if you just wanted a dynamic place in the document you can insert HTML of any kind. Note that a collection specializes in managing CSS and JavaScript, hence the outputJS method, but can also handle miscellaneous code for other purposes. There's also outputCSS, outputInlineJs, outputInlineCss, outputInline, and simply output. See:
http://php-phalcon-docs.readthedocs.org/en/latest/api/Phalcon_Assets_Manager.html
Then edit your ControllerBase.php to initialize the asset to an empty collection:
$this->assets->collection('footer');
(if you skip this step, your code will see errors if your top-level index.phtml file tries to output the contents of a non-existent collection if you never write to it, so always initialize it to an empty collection so it exists.)
Finally, from the controller you want to use, you'd do:
$this->assets->collection('footer')->addInlineJs($js);
where $js is your inline JavaScript, excluding the script tags.
You can also work with external JavaScript files by using
->addJs(...). By default it will be relative to your application directory, inserting a / automatically. If you dislike this functionality, you can set the second parameter to false then it will allow you to specify your own leading / or point your resources at another domain.
As far as the assets you can add to a collection, see:
https://docs.phalconphp.com/en/latest/api/Phalcon_Assets_Collection.html
Note that you can also add assets to your collection from your view and the changes would still appear in your top-most index.phtml.
It is also worth noting that you don't need to actually use collections, you can simply use assets without a collection, but I think collections are more powerful in that you get to name them so there's another level of separation in case you need to manage more than one kind of collection of data.
In terms of what you're trying to accomplish, if you're just trying to give the user a message, this is what Phalcon's flash is for, not to be confused with Adobe Flash which is for playing videos. Phalcon's flash is for flashing messages to the user such as error messages, or your form submit successfully kind of messages. See:
https://docs.phalconphp.com/en/latest/reference/flash.html
If you're still confused what flash is, a demo of what it's output is, you can see in the screenshot here: https://docs.phalconphp.com/en/latest/_images/invo-2.png
That is output of ->error(...) and ->notice(...) respectively. The flash component keeps track of a collection of the messages you'd like to show the user. Then once you're ready to display them to the user you'd call:
<?php echo $this->flash->output(); ?> from your view. It is best to make this call from your top-most template or a template which is always included in your top-most template such as your navigation template so you can easily display messages to the user. It is also useful for debugging. I'd suggest using twitter bootstrap for styling the flash output.
Some sample applications which you might find useful:
https://github.com/phalcon/invo
https://github.com/phalcon/vokuro
https://github.com/phalcon/website
https://github.com/phalcon/forum
Further reading:
https://docs.phalconphp.com/en/latest/index.html

MVC Routes & Subfolders

I'm trying to figure out a way to customize a Route that will allow me to use a subfolder within a particular View folder.
I have a Controller (FinanceAdmin) and a View folder (\FinanceAdmin) which contains a number of Views. Within that view folder, I have a lot of stand alone chart Views (Chart1, Chart2...Chart50, etc...) which I include as Partials on various View pages. To clean things up in my file/organizational structure, I would like to set things up like this:
I know I can use Areas to separate different parts of my application but that's not really what I'm looking for. I want to be able to create a custom Route so that, in my controller, I can simply return:
return View(chartdata);
instead of
return View("~/Views/FinanceAdmin/Chart/_Chart1.cshtml",chartdata);
Is that possible with a generic route (so I don't have to create one for each file)? I'd rather not write a custom view engine just for this unique circumstance.
I am afraid that this is not possible with a route. The routing engine finishes his responsibility at the time he finds (or doesn't find) a controller action to be executed given some request url.
Resolving views is purely the responsibility of the view engine. So if the conventions built into the view engine you are using do not meet your specific requirements, customizing this view engine is the right way to go.

Where is the equivalent of WebForms' Master Page codebehind files in ASP.NET MVC?

Today is my first day working with MVC and I am trying to convert my existing Web Forms website into an MVC 4 site.
I have done some reading and am starting to understand how things work but one thing I can not figure out is for the new Layouts (replacing MasterPages) where is the equivalent to the codebehind file? In my current site I have a Master Page that defines the general look and feel but also runs some code in the codebehind to changes a few things dynamically (for localization and DB generated menu system).
So now that I am using MVC and Layouts I can not figure out where I would code all that at, can anyone please point me in the right direction?
(I know MVC does not have code behinds it uses controllers for it.)
As you Know MVC is three layer architecture.
Model
View
Controller
Model is the data entities. You need to store, or show the data.
Views are the html or presentation layer which would be rendered to users.
Controller are the code behind file all of your code would go in controller. It gets data from Models and apply business logic and then pass to views to show or get updated data from view and pass to models and then save to database.
_layout.cshtml file is present at path of ~/Views/Shared/_Layout.cshtml. It is master-page in mvc. You would see your partial-views contains
Layout = "~/Views/Shared/_Layout.cshtml";
this line at top of page. You can change master-page for any views and you can have multiple Layouts.
Layout contains many partial-views like left-navigation, Top-Navigation and content. each of which can be customized from controller.
Here are some links might help you:
MVC Tutorials
Introduction to MVC
Create a Base Controller class and make all your controllers inherit from it.
The MVC equivalent of WebForms' Master Page codebehind is then this Base Controller, where you can put code you need for multiple controllers.
How can I execute common code for every request?
You can't find any examples of what you're trying to do, because that's not how it's done in MVC. There is no equivalent to code behinds.
You're "trying to do" the wrong thing. MVC layouts are simply template files. They have no code behind, and they should have no functionality besides simple display logic.
MVC is a different paradigm from WebForms. You don't use have server-side controls like WebForms. Therefore the idea that you have content in the layout that does it's own thing violates the MVC principles.
You're basically stuck in what's known as the XY problem. That's where you are trying to achieve certain functionality X, and you believe to do that you need to do Y, so all you ask about is Y... when X is what you really need to be asking about.
Please explain the actual thing you are trying to do, and don't assume that it must be done in the way you've always done it. For instance, if you want to localize something, then ask how to localize something. If you want dynamic content somewhere, ask how to do that, but you need to be more specific about these individual problems, and not just gloss over them as you have done here.

What should I prefer to use widget or renderPartial in Yii's view?

I am confused when I should use a custom widget or renderPartial in my view files. Sometimes I use widget and sometimes I use renderPartial.
Widget
You use widget when your application logic is defined in a separate CLASS file and the logic is somehow separated and standalone.
Widget's are chosen when the functionality is repeatedly used elsewhere, on lot of pages.
renderPartial
You use renderPartial for VIEW files that you want to embed into something bigger, or when you want to print something without using the application layouts.
renderPartial is chosen when all the variables it need to access are already prepared in the current action.
Widget
You can use widget when your site has some common part like header and footer or sometime some kind filter which require on every page of site.
renderPartial
Take example of search form of yii crude which is called by using renderPartial because that serach form is changing according to requirement of pages.
Sorry for english.

Keeping DRY with progressive enhancement

I'm building a website with very small amounts of Javascript, just to add things to the page (like a contact form) without having to go to a new page.
I understand that I should build the contact page anyways (just in case the user doesn't have javascript turned on) and then use javascript if they've got it.
So where do I store the HTML for the form if I don't want to have it in two places?
(Normally I'm not so picky, but I'm curious on this one.)
If you have access to a server-side language, you can keep a separate snippet of the form in an external page. Then, you can include the snippet into the HTML content page with an appropriate include call. This has the added benefit that, for your JavaScript, you can pull the contact form from this snippet file using AJAX. In fact, many plugins allow you to display DHTML windows with HTML content. For example, check out ThickBox.
Without a server-side language, you can do something similar with frames. Just display the form snippet in a frame when you need to reference it. Personally, I don't like frames very much, so this isn't a very attractive solution for me, but you can use it if you choose (and style the frames appropriately).
Just put your HTML for the contact form in a .html file. Assuming you're using PHP or something, just include the file in your contact page and include it in the section for your dynamic contact form. The form should still submit to the same server-side page and have the same look and feel..
e.g. contactForm.html
<div class="contact-form">
<input ....>
</div>