Welcome Guest, Not a member yet? Register   Sign In
Now I just know I'm being a newbie here!
#1

[eluser]deadfrog[/eluser]
Hi! I've noticed I'm calling a lot of the same things in my controller, for example I'm creating a data array which passes title,meta keywords & descriptions, and loads in my 'includes'. What I thought I could do would be to create a new controller, which the other controllers could inherit from. This main controller would store (for example) a default title,meta keywords & description and also enable me to pass new 'includes' to every page in one go.

It doesn't work (no error, just a blank page) and I'm sure the reason for that is my lack of expertise and failure to connect it up properly - if someone could advise me that would be terrific.

Here's my application controller:

Code:
<?php
class Application extends Controller {

    $data['html_title'] = 'Default Title';
    $data['meta_description'] = 'Default Description';
    $data['meta_keywords'] = 'Default Keywords';
    $data['html_header'] = $this->load->view('includes/html_header', $data, TRUE);
    $data['header'] = $this->load->view('includes/header', '', TRUE);
    $data['footer'] = $this->load->view('includes/footer', '', TRUE);

    function Application()
    {
        parent::Controller();            
    }
}
?>

And here's my home controller:

Code:
<?php
class Home extends Application {

    function Home()
    {
        parent::Application();
    }
    
    function index()
    {
        $data['html_title'] = 'This page Title';
        $data['meta_description'] = 'This page Description';
        $data['meta_keywords'] = 'This page Keywords';
        $this->load->view('home',$data);
    }
}
?>
#2

[eluser]Seppo[/eluser]
Are you including the Application controller file? You can use a hook (to do it automaticly), or just include it in each file

Code:
<?php
require_once(APPPATH . 'controllers/Application.php');
class Home extends Application {

    function Home()
    {
        parent::Application();
    }
    
    function index()
    {
        $data['html_title'] = 'This page Title';
        $data['meta_description'] = 'This page Description';
        $data['meta_keywords'] = 'This page Keywords';
        $this->load->view('home',$data);
    }
}
?>
#3

[eluser]Clooner[/eluser]
I do it this way. I create a library my_controller.php

Code:
class MY_Controller extends Controller {
protected $data;

function MY_Controller()
{
  parent::Controller();
  $this->data['title']    =    "Dafault";
  $this->data['desc']    =    "haha";
}

and then use in my controllers

Code:
class Home extends MY_Controller {
function Home()
{
  parent::MY_Controller();    
}

function index()
{
  // change the data var
  $this->data['title'] = "Home";
  // to load the view
  $this->load->view('shop',$this->data);
}
#4

[eluser]deadfrog[/eluser]
Thanks for the replies guys. I didn't have application.php included, but now I have it still isn't working. In fact neither way works which suggests something else is up here..

Gah if I can just crack this I'll be sorted!
#5

[eluser]Seppo[/eluser]
Why don't you enable display_errors using .htaccess or setting it in the codeigniter index.php so you can find out what's wrong easier?
#6

[eluser]deadfrog[/eluser]
Well this is why I think it's just something daft I'm doing - my errors are set to error_reporting(E_ALL);!
#7

[eluser]Seppo[/eluser]
but display_errors can be off and the errors are only logged and not display. Try creating a .htaccess file with
Quote:php_flag display_errors On
#8

[eluser]deadfrog[/eluser]
Tried and no change Sad
#9

[eluser]Clooner[/eluser]
Could it be something in your routing?
#10

[eluser]deadfrog[/eluser]
I just kinda started from scratch and it still isn't working.

Here's my new 'main' controller:

Code:
<?php
class Application extends Controller {    
    
    function Application()
    {
        parent::Controller();    
        $data['html_title'] = 'Default Title';
        $data['meta_description'] = 'Default Description';
        $data['meta_keywords'] = 'Default Keywords';
        $data['html_header'] = $this->load->view('includes/html_header', $data, TRUE);
        $data['header'] = $this->load->view('includes/header', '', TRUE);
        $data['footer'] = $this->load->view('includes/footer', '', TRUE);
    }
}
?>

And here's the controller that's calling it.

Code:
<?php
require_once(APPPATH.'controllers/application.php');
class Home extends Application {
    function Home()
    {
        parent::Application();
    }
    
    function index()
    {        
        $this->load->view('home',$data);
    }
}
?>

The errors I'm getting are undefined variables, so nothing in that data array in main is being received by the calling controller.




Theme © iAndrew 2016 - Forum software by © MyBB