Welcome Guest, Not a member yet? Register   Sign In
Problem with views
#1

[eluser]invi[/eluser]
Hello everyone!

Up to this moment I have always found solutions for my problems in archived topics.

I have a template of my new application which consists of header, content divided into 3 columns [left, main, right] and footer. In the left column there is always login form [div block] but it should only be shown when user is not logged in. After logging there should be shown user menu. I do not know how to organize my views in this case. I was looking for solutions but I didn't find anything suitable for me.

Please help me to deal with this problem because I am new to CI and MVC in general and not everything is clear for me yet.
#2

[eluser]bastones[/eluser]
Hi invi,

CodeIgniter is extremely easy to use and reading the user guide is essential to development with it. It is good to learn first before you head start into your own project, therefore most of your first questions will be answered as you read the user guide. You can read the user guide here and specifically from the Table of Contents its good to start at "CodeIgniter URLs".

MVC is an architecture approach which seperates code from HTML. All your PHP code is seperated from the design, structure and HTML that you generate for your end-users to see; and this is the underlying thing of the MVC approach. All PHP code goes in Controllers, and all your HTML code, and design, etc., goes in View files. It really is difficult to explain unless you see for yourself so its good to look at the beginning sections of the user guide and it would be good to check out this beginner introduction video tutorial to CI.

There's no need to rush into learning CI, its pretty simple and obviously a bit of PHP experience really is essential. Check out that video tutorial then beginning sections of the user guide. Whatever questions you then have, feel free to post them on the forum Smile.
#3

[eluser]invi[/eluser]
I have some theoretical knowledge of MVC and I think I understand enough to start doing something practical.

I started a week ago from creating the "welcome" view [the whole template in one view] to check how it works. Then I created Registration Controller, Model and Views to it and I adjusted my link to this registration section as it is written in user guide [site_url()] and it works fine.

Now I want to deal with login form. I know how to create it with form helper but I don't know how to create something like subview in which this login form should be shown on every page when user is unlogged. In template it is in the left column [as I mentioned in my first post]. Simply do I have to create a view in, for instance, User Controller [login form if unlogged or user menu if logged] or create a [sub]view in the main Controller? I went through user guide and I didn't find solution for this. How to load a view in another view under some conditions? I know that I should use $this->load->view(...) but it should be done in controller and I don't know how to do this. I've read in user guide that I cand load multiple views and use if conditions but how does the CI know that I want to load for instance login form exactly into this div block in left column? Please get me an idea how to do it.

I have also additional question. Is this correct that I load header and footer view with $this->load->view() in main view? Or do I have to load it in Controller in order: header,main,footer? Is this first way is in accordance with CI and MVC concept?
#4

[eluser]@li[/eluser]
Just do something like this in your header.php,just above the column where you display the login form or the user panel menu:
Code:
<div>
   &lt;? if (! $this->auth->isLoggedIn()):?&gt;
     &lt;? $this->load->view('login_form');?&gt;
   &lt;? else: ?&gt;
     &lt;? $this->load->view('user_menu');?&gt;
   &lt;? endif;?&gt;
</div>

Note that you'll have to change the line
Code:
$this->auth->isLoggedIn()
with your own function that would return true if someone is logged in and false if they're not
#5

[eluser]invi[/eluser]
@li thanks so much for this example. But I have some doubts about it.

Shouldn't I manage such view loading in the Controller or your example is just consistent with CI and MVC?

My second doubt is that my login form/user menu is in left column in the content part and not in the header? Should I just divide left column form the content and do the same like in your example with header?

I am new to MVC and please forgive me such trivial questions for you. I appreciate every help from you Smile
#6

[eluser]therealmaloy[/eluser]
invi

same as you before, i was looking into such idea, a very well laid out authentication mechanism. It's not advisable to defect from the good rules of MVC, such as mixing up the implementation of suppose to be views inside controllers.

then i stumbled with Linkster which can be found at http://www.codeigniterdirectory.com -> full applications.

check out the implementation of the templating system, it's quite a beauty, applause for the team. it really handles authentication, such login transitions very well. I have properly figured out the ins and outs of the authentication system and I am using the same principles as foundation for my applications.

sadly it's still v1.0.0 and using CI v1.6.x, there's no update for this one right now though i managed to port this to handle CI v1.7.0. Smile
#7

[eluser]invi[/eluser]
Thanks for the link. I found there Redux Auth libs, very helpful. Smile




Theme © iAndrew 2016 - Forum software by © MyBB