Welcome Guest, Not a member yet? Register   Sign In
Controller, view question...
#1

[eluser]mrh[/eluser]
Hello all,

Please forgive if this has been asked (probably many times)... I've searched and not seen the answer to this very simple and probably idiotic question... I do understand MVC and I've been using OOP for many years but I am new to PHP and CI... My question is this:

For each view, I have a controller. for example:

adminlogin - controller
adminlogin_view - view

adminportal - controller
adminportal_view - view

I also have a data model for getting stuff from the database. My question is this:

In my adminlogin controller index() function I load the adminlogin_view. This displays a username/password field. The submit button is set to call adminlogin->submit(). In this function I see if the username and password are correct using a function from the model. If they are correct I want to send them off to the adminportal view where they have functions to update/edit and do admin type stuff.

So in submit() I am doing the following if they validate ok (correct username/password)

Code:
$name = $this->input->post('userName');
$pass = $this->input->post('userPass');
$adminID = $this->data_model->checkAdminlogin( $name, $pass );
  
if( $adminID>0 )
{
// This session data is vital to allow the admin_portal to function
$this->session->set_userdata( 'axID', $adminID );
  
// Count applications that exist
$data = array();  
    
// Get application data
$appCount = 0;
$appData = array();
$this->data_model->getApplicationsData( $appCount, $appData );
          
$data['appCount'] = $appCount;
$data['appData'] = $appData;
        
// Setup menus for this view - No highlight
$menuData['menus'] = self::constructMenu( "" );
  
// Load the view
$this->load->view( 'header', $menuData );
$this->load->view( 'adminportal_view', $data );
$this->load->view( 'footer', $menuData );
}

This is all well and good but it is a duplicate of the data gathering code in the index() function of the adminportal controller! IE I have to have this code in two places! First I need to have it here so that when I vector the user to the adminportal the data is loaded. I also have to have this identical code in the index() function of the adminportal because after they do a particular function and they want to come back to the portal I need to load the same data.

I guess in simple terms I'm wondering what I'm doing wrong. When I execute the lines:

Code:
// Load the view
$this->load->view( 'header', $menuData );
$this->load->view( 'adminportal_view', $data );
$this->load->view( 'footer', $menuData );

It seems that the adminportal controller is not loaded nor is index called. This kind of makes sense because the current controller executing is "adminlogin". What I think I want to do is to let the current adminlogin controller quit and give control the the adminportal controller so it can load the correct data and move forward.

Hopefully I'm just being stupid here and totally missing something but I've been doing this based on the advice in the myriad tutorials. It seems to me that what I really want to do and I have no idea how to do it is instead of just loading the "adminportal_view" from the current controller is to load the adminportal controller and call index().

Logically this would keep the data gathering code grouped with the controller and view.

What am I missing? Is there a way instead of just loading the view to effectively say:

Code:
load_controller( adminportal );

and have that pretty much act as if they called it directly in the url as in www.mysite.com/index.php/adminportal?

Again I apologize for this question. I've probably just missed something and I have been reading the UG and searching about. Sometimes though asking the experts is the only way.

#2

[eluser]Aken[/eluser]
I never mix the log in/out features with actually displaying content. For example, my admin controller would look something like this:

controller: admin.php

index() :: Check if logged in. If YES, show the page. If NO, redirect to login().

anothermethod() :: Another admin page. Do the same as index().

login() :: Check if logged in. If YES, then redirect to index() (we don't need them to log in twice). If NO, then show login form. Login form sends data back to this same method, not another. It processes supplied POST data and checks for a valid login. If YES, redirect to index(). If NO, show an error and the form again.
#3

[eluser]mrh[/eluser]
Aken,

Thanks for the description. I'll need to go through it carefully so as not to misunderstand... But I'm not mixing login and display content....

IE if you consider that my "adminlogin" controller and view does nothing more than get the username and password then set a session var with a login ID. Then the idea would be to vector control to "admin portal subsystem". IE the only thing that would have would be (pseudo code... not real PHP)

Code:
adminID = get session ID( "axID" );
if( adminID>0 )   // /then they are logged in
{
   show the adminportal view
}
else
{
   not logged in... show them some other page
}

So the portal code just checks if logged in and that's it. My plan is that this would be like a star network where the portal would have links and stuff to take them to different admin functionalities, when done they would come back to the adminportal main page and can go off and do something else. The admin portal main page would have a logout link which would kill the session value and take them someone like the main page or whatever.

But my point is not about that design... it is about the fact that on my adminportal main page I want to have some data they would like to see. Like number of applications pending, number of projects etc. I also plan to have dropdown lists of the apps and projects so they can select one and go edit it or update it. For this to work I need to use the model to get that data and pass it to the portalview as shown in the tutorials:

Code:
// Load the view
$this->load->view( 'header', $menuData );
$this->load->view( 'adminportal_view', $data );
$this->load->view( 'footer', $menuData );

IE $data needs to be populated. It seems to me that if I want people to be able to work from the portal page and see this data that I need this code which populates $data in two places... The first is in the login area where I validate them then send them to the portal view. I also need it in the controller of the portal so that when a link takes them back to the main portal page the data can be reloaded and redisplayed.

It is my hope I'm missing something. I'd dearly love to put this code in just one place. Maybe the answer is to move more of it to the model so in both places I do something simple like:

Code:
// Setup menus for this view - No highlight
$menuData['menus'] = self::constructMenu( "" );

$data = $this->data_model->getMainPortalPageData( );
          
$this->load->view( 'header', $menuData );
$this->load->view( 'adminportal_view', $data );
$this->load->view( 'footer', $menuData );

At least this moves all of the logic to construct the array needed for the view into the model so there is just ONE copy of the code making that array. Then in my portal controller index I'd have the same code as above.

I guess coming from some other OOP MVC implementations there was always a way to go from one controller to pass control to a new controller. That is why I gave the example above of loading a "controller". IE instead of $this->load->view() is there way way to do:

$this->load->controller( "adminportal" );

And that would in effect be the same as if the user had clicked on a link: www.mysite.com/index.php/adminportal which would invoke the index() function of that controller class which is where I would place the above code just once.

It's been a long day... I apologize if I'm not making sense. This morning my SSD experienced the amazing 5184 hour bug. (crucial M4). I had to disassemble my laptop to be able to update the firmware and that put me 4 hours behind on what I'd hoped to do.
#4

[eluser]Aken[/eluser]
[quote author="mrh" date="1343696489"]Aken,

Thanks for the description. I'll need to go through it carefully so as not to misunderstand... But I'm not mixing login and display content....[/quote]

Yes you are - in the submit() method example you gave, you're loading views after validating a user log in.

As I said in my example, I just redirect the user to the appropriate URL, whether that means sending them to login page because they're not logged in, or sending them back to a "portal" page upon successful login.
#5

[eluser]mrh[/eluser]
Well let me study your example. Thanks! I guess I was looking at it a little differently but I see your point.
#6

[eluser]mrh[/eluser]
[quote author="Aken" date="1343695465"]I never mix the log in/out features with actually displaying content. For example, my admin controller would look something like this:

controller: admin.php

index() :: Check if logged in. If YES, show the page. If NO, redirect to login().

anothermethod() :: Another admin page. Do the same as index().

login() :: Check if logged in. If YES, then redirect to index() (we don't need them to log in twice). If NO, then show login form. Login form sends data back to this same method, not another. It processes supplied POST data and checks for a valid login. If YES, redirect to index(). If NO, show an error and the form again.[/quote]

Ok getting into symantecs... I see your point... But I could argue that the index() method of admin would then be responsible for loading two different views depending upon login state... But ya... I think that makes more sense.

Ok time to re-organize! Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB