Welcome Guest, Not a member yet? Register   Sign In
Multi controller in one view
#1

[eluser]Alak[/eluser]
Hi,

(sorry for my bad english, i'm french)

This is my problem:

-application
+...
-controller
home.php
poll.php
admin.php
+...
+...
+...
-views
home.php
admin.php
header.php
footer.php
sidebar.php
poll.php


When a user come on my website :

The first page displayed is home :

in home I include header.php, sidebar.php and footer.php

the problem is : sidebar.php need to include poll.php (controller)
(sidebar is include on all of my website page)
actually I use url including but this is a bad solution.

Do you have a better way to do this?

Thank you.
#2

[eluser]theprodigy[/eluser]
Whatever you have in poll.php that needs to be accessible to other controllers, should probably be moved to a helper or library.
#3

[eluser]Alak[/eluser]
hmmm, i think it's not the best way.

look the probleme from another point of view :

replace poll.php by login.php (a classic login system), if i'm not logged, it display a form, if i'm logged it display some infomation of my profile like my prive message box or other things.

to do this :

I creat a controller "login.php" with some action like identification, logout and ....

i need to see my login box on all of my page. How i can do this?

I don't know if I'm understandable ?
#4

[eluser]Tom Schlick[/eluser]
[quote author="theprodigy" date="1264300007"]Whatever you have in poll.php that needs to be accessible to other controllers, should probably be moved to a helper or library.[/quote]

I agree. If you need to load the login box do it by including the login view. which submits to its controller
#5

[eluser]Alak[/eluser]
oki for the form it's possible, but when i'm logged i need to display some information

_______________________
| |
| Hi, Alak |
| 5 message(s) |
| in your box |
|_______________________|

in my login_controller.php it load my login_view.php and if i'm logged he desplay the box like at top if i'm not he display the form.
How can I including this box in my view? If I can't, someone can show me how he manage this situation?

I think i've a bad use of controller. Look, if in my menu i've 3 button like : News FAQ Contact, i creat 3 controller, one for each my website page. and if I ve a functionnality displayed on all of my page like login systeme or poll system i creat a controller too.

Actually on my point of view i've 2 solution :

1) creat just one controller with the action of News FAQ Contact Poll and Login

2) Rewrite Login or poll controller in News, FAQ Contact controller.

But i Think the both are false, I ve to learn the good method Smile

Thank you for your help Smile
#6

[eluser]Tom Schlick[/eluser]
Code:
if($logged_in == TRUE)
{
$this->load->view('pagehere');
}
else
{
$this->load->view('login');
}

this would be in your view file (sidebar from what it sounds like) and $logged_in would be a variable you would set from your controller and pass into the view
#7

[eluser]theprodigy[/eluser]
[quote author="trs21219" date="1264306501"]
Code:
if($logged_in == TRUE)
{
$this->load->view('pagehere');
}
else
{
$this->load->view('login');
}

this would be in your view file (sidebar from what it sounds like) and $logged_in would be a variable you would set from your controller and pass into the view[/quote]

or in your controller:
Code:
if ($this->session->userdata('logged_in'))
{
    $data['view'] = 'info_box';
}
else
{
    $data['view'] = 'login_form';
}

then in your view:
Code:
$this->load->view($view);

Same logic, only moves the decision making to the controller, rather then the view. I would actually suggest putting this in the constructor, so you don't have to call it all the time manually.
#8

[eluser]Alak[/eluser]
thanks everyone

I understand right now Smile
#9

[eluser]Tom Schlick[/eluser]
[quote author="theprodigy" date="1264309796"][quote author="trs21219" date="1264306501"]
Code:
if($logged_in == TRUE)
{
$this->load->view('pagehere');
}
else
{
$this->load->view('login');
}

this would be in your view file (sidebar from what it sounds like) and $logged_in would be a variable you would set from your controller and pass into the view[/quote]

or in your controller:
Code:
if ($this->session->userdata('logged_in'))
{
    $data['view'] = 'info_box';
}
else
{
    $data['view'] = 'login_form';
}

then in your view:
Code:
$this->load->view($view);

Same logic, only moves the decision making to the controller, rather then the view. I would actually suggest putting this in the constructor, so you don't have to call it all the time manually.[/quote]

i was basing my code on him using some sort of structured template. if you use the sidebar on every single page then there is no point to include it in every controller. that is why i include it on the view level. and really the if/else dont have to stay in the controller.
#10

[eluser]theprodigy[/eluser]
understandable.

To be completely honest, I use the application layout/template style. And, I would personally put the sidebar into a partial, and have the layout.php view call the sidebar.

The sidebar may actually be somewhat of a template itself that calls other partials depending on what you have setup (if it's a sideBAR and not just a sideITEM). But, in that setup, I would still put the if logic into the constructor of the MY_Controller, since logged in status is something your going to need to know anyway (can't access certain pages if not logged in, show login form if not logged in, etc). No point in running the same basic if statement multiple times, if you can call it once and set everything you need.




Theme © iAndrew 2016 - Forum software by © MyBB