• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to accomplish this

#1
[eluser]jfenety[/eluser]
How can I accomplish this problem without repeating a bunch of code. I have a view, header.php that is loaded from the controller $this->load->view(header.php) for each page that needs the header. Example, I have three controllers, one for main, one for about, and one for contact. The code has to be in each of these.

I want to have a Welcome, user at the top of the screen. I am using tank auth, in the controller I call isloggedin and then pass the username to the view from the controller. Is there a way I can do this without having to put this statement into every controller that needs the header.php?

Could I call the tank auth library from the view, and get the user name in the view? Or would this introduce problems, or ruin the MVC architecture?

Jfenety

#2
[eluser]InsiteFX[/eluser]
Create a template_view.php
Code:
<?php echo $this->load->view('header_view);?>
<?php echo $this->load->view('content_view);?>
<?php echo $this->load->view('footer_view);?>

In your Controller you can do this:
Code:
$data = array()

$data['welcome'] = 'assiging your tank auth user name here etc';
// add more $data parametes here! You can change them in your controllers.

// this will pass $data to all the views in template_view.php
$this->load->vars($data);

$this->load->view('template_view');

InsiteFX

#3
[eluser]jfenety[/eluser]
Thanks for your help,

Should I create a base_controller that all my other controllers inherit from? That way, I would just have the welcome message code in the base_controller, and then would never have to repeat it?

#4
[eluser]InsiteFX[/eluser]
Yes, that would be the way to go!
Code:
// MY_Controller - application/core/MY_Controller.php
class MY_Controller extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }
}

// Admin_Controller - application/core/Admin_Controller.php
class Admin_Controller extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }
}

// Public_Controller - application/core/Public_Controller.php
class Public_Controller extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }
}

// You will also need to add this to the bottom of - application/config/config.php
/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon.
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| If using HMVC you do not need this! HMVC will autoload.
|
| Place this code at the bottom of your application/config/config.php file.
*/
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        @include_once(APPPATH . 'core/' . $class . EXT);
    }
}
Now you can extend all your other Controllers from either the Admin_Controller or Public_Controller

Admin_Controller - for backend Dashboard etc.
Public_Controller - for frontend.

InsiteFX

#5
[eluser]jfenety[/eluser]
Thanks a lot,

I am working on a site similar to kijiji, but for only a specific need and for a specific market base. Its sort of a pet project, to learn php better. This gives me a great start to understand the code igniter framework, and to tinker away for a while in php.

jfenety.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.