Welcome Guest, Not a member yet? Register   Sign In
Add $this->load->view to all functions?
#1

[eluser]Fredrik-s[/eluser]
Hello,

I'm totally new at using CodeIgniter and I have a question about the $this->load->view command.

I have done a header and a footer, which I want to add to my code through the controller file. The code I am using is a auth library found here at codeigniter.com. I have read the documentation and what I understood is that I should only be in need to add the implementation of the header and footer in the index() function and after that all the pages which is going through this controller will show up with my header and footer. But this does'nt seems to work. If i add the code:
$this->load->view('header');
$this->load->view('footer');

to all my functions it appears right with the header and footer implemented. But as it could be many different functions I'm wondering if I most add the header and footer code to all thoughts? Is'nt there a simple way that all the new functions I'm adding will have the header and footer in them per default?

Code:
//redirect if needed, otherwise display the user list
    function index()
    {    
        $this->load->view('header');
        if (!$this->ion_auth->logged_in())
        {
            //redirect them to the login page
            redirect('auth/login', 'refresh');
        }
        elseif (!$this->ion_auth->is_admin())
        {
            //redirect them to the home page because they must be an administrator to view this
            redirect($this->config->item('base_url'), 'refresh');
        }
        else
        {
            //set the flash data error message if there is one
            $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

            //list the users
            $this->data['users'] = $this->ion_auth->get_users_array();
            $this->load->view('auth/index', $this->data);
        }
        $this->load->view('footer');
    }

    //log the user in
    function login()
    {
        $this->load->view('header');
        $this->data['title'] = "Login";

        //validate form input
        $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run() == true)
        { //check to see if the user is logging in
            //check for "remember me"
            $remember = (bool) $this->input->post('remember');

            if ($this->ion_auth->login($this->input->post('email'), $this->input->post('password'), $remember))
            { //if the login is successful
                //redirect them back to the home page
                $this->session->set_flashdata('message', $this->ion_auth->messages());
                redirect($this->config->item('base_url'), 'refresh');
            }
            else
            { //if the login was un-successful
                //redirect them back to the login page
                $this->session->set_flashdata('message', $this->ion_auth->errors());
                redirect('auth/login', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries
            }
        }
        else
        {  //the user is not logging in so display the login page
            //set the flash data error message if there is one
            $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

            $this->data['email'] = array('name' => 'email',
                'id' => 'email',
                'type' => 'text',
                'value' => $this->form_validation->set_value('email'),
            );
            $this->data['password'] = array('name' => 'password',
                'id' => 'password',
                'type' => 'password',
            );

            $this->load->view('auth/login', $this->data);
        }
        $this->load->view('footer');
    }
#2

[eluser]davidbehler[/eluser]
Possible solution would be to write your own function that you call instead of $this->load->view() in your controller. I would extend the CI_Controller class and put that function there:
Create a file called MY_Controller in application/core with this content:
Code:
class MY_Controller extends CI_Controller {
  function __construct() {
    parent::__construct();
  }

  function load_view($view_file, $data) {
    $this->load->view('header');
    $this->load->view($view_file, $data);
    $this->load->view('footer');
  }
}

And now you only have to update your controllers to extend MY_Controller instead of CI_Controller and in your functions you would call $this->load_view('auth/login', $this->data); and remove the $this->load->view('header') and $this->load->view('footer') calls.
#3

[eluser]Fredrik-s[/eluser]
[quote author="waldmeister" date="1303655570"]Possible solution would be to write your own function that you call instead of $this->load->view() in your controller. I would extend the CI_Controller class and put that function there:
Create a file called MY_Controller in application/core with this content:
Code:
class MY_Controller extends CI_Controller {
  function __construct() {
    parent::__construct();
  }

  function load_view($view_file, $data) {
    $this->load->view('header');
    $this->load->view($view_file, $data);
    $this->load->view('footer');
  }
}

And now you only have to update your controllers to extend MY_Controller instead of CI_Controller and in your functions you would call $this->load_view('auth/login', $this->data); and remove the $this->load->view('header') and $this->load->view('footer') calls.[/quote]

Thanks for answer!

I run in to a problem though. I have added the code you stated above into my /application/core -folder. Named the file MY_Controller.php.

And in my auth.php file in the folder /application/controllers/ I have changed the following code on the top to extend the MY_Controller class:
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! class_exists('Controller'))
{
    class Controller extends [b]MY_Controller[/b] {}
}

class Auth extends Controller {

From this long I have come I got the following error message stating that it does'nt find the MY_Controller class

Fatal error: Class 'MY_Controller' not found in /home/XXXXX/application/controllers/auth.php on line 8

Of course it can't find it because the file MY_Controller.php is in the core folder and not the controllers. How come this error shows up?
#4

[eluser]davidbehler[/eluser]
Are you using CI 2.X or 1.7? If you are using 1.7 then you have to put the MY_Controller file into application/library and have it extend Controller instead of CI_Controller.
#5

[eluser]InsiteFX[/eluser]
To use a MY_Controller in CI 2.0.+ from the core you need to include the following:
Code:
/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon. New Version!
| -------------------------------------------------------------------
|
| 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)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }

        elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
        {
            include $file;
        }
    }
}

InsiteFX
#6

[eluser]davidbehler[/eluser]
Actually you don't. At least not that I'm aware of. I'm using CI 2.0 with MY_Controller in application/core and it works just fine without that function.
#7

[eluser]Fredrik-s[/eluser]
Thanks for the answers!

I tried to add the code that InsiteFX told about in the file /application/config/config.php, added it to the bottom. I'm afraid that didn't help either.

I'm using the latest version of CI, version 2.0.2.

Any other suggestions on what could be wrong?
#8

[eluser]InsiteFX[/eluser]
You cannot do this Controller is a CI reserved word!
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! class_exists('Controller'))
{
    class Controller extends MY_Controller {}
}

// should be something like this! application/core/Admin_Controller.php
class Auth extends Controller {
<?php defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! class_exists('Admin_Controller'))
{
    class Admin_Controller extends MY_Controller {}
}

class Auth extends Admin_Controller {

InsiteFX
#9

[eluser]Fredrik-s[/eluser]
[quote author="InsiteFX" date="1303697189"]You cannot do this Controller is a CI reserved word!
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! class_exists('Controller'))
{
    class Controller extends MY_Controller {}
}

// should be something like this! application/core/Admin_Controller.php
class Auth extends Controller {
<?php defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! class_exists('Admin_Controller'))
{
    class Admin_Controller extends MY_Controller {}
}

class Auth extends Admin_Controller {

InsiteFX[/quote]

Thanks for answer.

I'm using this authentication library http://codeigniter.com/wiki/Ion_Auth_-_L...th_System/

Strange that it is coded with reserved name. I think I will change library.
#10

[eluser]InsiteFX[/eluser]
I would re-download Ion Auth, It sounds like you have an older version!

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB