Welcome Guest, Not a member yet? Register   Sign In
How to use functionality of another controller (render its view inside mine)?
#1
Question 

Hi, everybody!
I need to render the login form of an authentication library inside my own view. It is not possible to simply load view 'login_form', because there are many parameters it needs. That job is performed by corresponding function of the auth controller. But it became a disaster trying to load that controller from inside my view!
load_class(...) says it cant find the class, whatever paths I tell it...
require(...) leads to the chain of 'undefined property' (hadr to figure out the reasons)...
PHP Code:
<div id="userbar">
 <?
php if ($this->tank_auth->is_logged_in()): ?>
 Hi, <strong><?=$this->tank_auth->get_username().' ('.$this->tank_auth->get_user_id().')';?></strong>!<br />
 <a href="/index.php/auth/logout">Logout</a>
 <?php else: ?>
 <?php  // all these doesnt work, causes chain of errors
 
require_once('application/controllers/auth.php'); // success!
 
$this->AuthController = new Auth// success!
 
$this->AuthController->login(); // this should render a login form here, but errors happen inside auth library/controller
 
?>
 <?php endif ?>
 </div> 

What is the best way to get such things done???
Reply
#2

(10-12-2015, 10:53 AM)Kulavert Wrote: Hi, everybody!
I need to render the login form of an authentication library inside my own view. It is not possible to simply load view 'login_form', because there are many parameters it needs. That job is performed by corresponding function of the auth controller. But it became a disaster trying to load that controller from inside my view!
load_class(...) says it cant find the class, whatever paths I tell it...
require(...) leads to the chain of 'undefined property' (hadr to figure out the reasons)...

PHP Code:
<div id="userbar">
 <?
php if ($this->tank_auth->is_logged_in()): ?>
 Hi, <strong><?=$this->tank_auth->get_username().' ('.$this->tank_auth->get_user_id().')';?></strong>!<br />
 <a href="/index.php/auth/logout">Logout</a>
 <?php else: ?>
 <?php  // all these doesnt work, causes chain of errors
 
require_once('application/controllers/auth.php'); // success!
 
$this->AuthController = new Auth// success!
 
$this->AuthController->login(); // this should render a login form here, but errors happen inside auth library/controller
 
?>
 <?php endif ?>
 </div> 

What is the best way to get such things done???

I suggest, you define all variables needed in your controller and call it to your views. Do not initialize your controller in your views.

Example:

to display a login form to your views


Code:
//in your controller yourcontroller.php

function some_method() {
   $data['is_logged_in'] = false; //sample the user is not logged in
   $data['login_form'] = $this->load->view('login', '', true);
   $this->load->view('main_view', $data);
}


Code:
//in your views main_view.php

<?php

   if(!$is_logged_in) {
      echo $login_form;
   }else {
      echo 'Welcome! you are logged in.';
   }
?>

This is a simple example, and I hope it may help to fix your problem.

God bless and more power!
Reply
#3

Instead of calling Tank-Auth's auth controller, use it as an example and add the necessary functionality into your base controller (usually MY_Controller). You really just need to setup your own login view, retrieve the values from the view, validate them, then pass them to the tank_auth->login() method.
Reply
#4

(10-13-2015, 01:09 PM)mwhitney Wrote: Instead of calling Tank-Auth's auth controller, use it as an example and add the necessary functionality into your base controller (usually MY_Controller). You really just need to setup your own login view, retrieve the values from the view, validate them, then pass them to the tank_auth->login() method.

Quote:lazyfox
I suggest, you define all variables needed in your controller and call it to your views. Do not initialize your controller in your views.

Doing so means:
1) Defining all variables and logic in my controller = duplicating code;
2) Moving that code to my controller = breaking another controller;
3) Moving necessary code to library/helper = breaking the concepts of MVC;
Besides, target controller may be a third-party component (as it is in my example), so changing it and those things above - are very bad practices, I believe!
Reply
#5

(This post was last modified: 10-14-2015, 05:02 AM by ivantcholakov.)

@Kulavert

What you want sounds like HMVC. I have never used Tank-Auth, so I can not be concrete here. I have found an old project that could give a hint: https://github.com/Abban/Tank-Auth-for-MX-HMVC

Edit: Search with "Tank-Auth HMVC" for checking about more solutions.
Reply
#6

(This post was last modified: 10-14-2015, 07:45 AM by mwhitney.)

(10-14-2015, 12:33 AM)Kulavert Wrote:
(10-13-2015, 01:09 PM)mwhitney Wrote: Instead of calling Tank-Auth's auth controller, use it as an example and add the necessary functionality into your base controller (usually MY_Controller). You really just need to setup your own login view, retrieve the values from the view, validate them, then pass them to the tank_auth->login() method.

Quote:lazyfox
I suggest, you define all variables needed in your controller and call it to your views. Do not initialize your controller in your views.

Doing so means:
1) Defining all variables and logic in my controller = duplicating code;
2) Moving that code to my controller = breaking another controller;
3) Moving necessary code to library/helper = breaking the concepts of MVC;
Besides, target controller may be a third-party component (as it is in my example), so changing it and those things above - are very bad practices, I believe!

If you don't want to move the code from the Auth controller, then you need your controller(s) to extend the Auth controller and make sure the Auth controller is auto-loaded.

However, it's possible that, at some point, you'll have to pull some of the original code from the Auth controller into a method within your own base controller just to be able to setup the data for a view without calling the original controller's method(s). Part of this is simply a design issue with the original component, and is why my original instinct was to treat the Auth controller as an example and the Tank_auth library as the important 3rd party component.
Reply
#7

(10-14-2015, 04:58 AM)ivantcholakov Wrote: What you want sounds like HMVC. I have never used Tank-Auth, so I can not be concrete here. I have found an old project that could give a hint: https://github.com/Abban/Tank-Auth-for-MX-HMVC

Edit: Search with "Tank-Auth HMVC" for checking about more solutions.

Ivan, now I believe HMVC is the right way to go! Starting to smoke manuals...
Mwhitney, I have read yours and kilishan's posts about Modules::Run(), etc.
Thank you all for co-operation Smile


By the way, I have found another solution for my certain example with Auth: I just script a client to load "index.php/auth/login" into the target DIV. Works fine! In a case when script fails there is still a "login" anchor in the target DIV.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB