• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Template Library and DX_Auth Integration

#1
[eluser]Mithun[/eluser]
I uses DX Auth and Template Library http://www.williamsconcepts.com/ci/codei.../template/

I need set up a layout, there there is a user area that is displayed only for logged users. I have created the curresponding view in views/user_area.php and this view shows logged users name.

For getting this I need to setup the following code in every controller constructor.
Code:
$this->load->library(array('template', 'DX_Auth'));
        $data ['user_name'] = $this->dx_auth->get_username();
        $this->template->write_view('user_area','user_area', $data);
How can I make this automated? editing the Template library code or setting up different layous for login and non-login pages?

this is my template layout file
Code:
<html>
   <head>
      <title><?php echo $title; ?></title>
      <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/template.css" />
   </head>
    <body>
        <div id="wrapper">
            <div id="header">
                <div id="user_area">
                    &lt;?php echo $user_area;?&gt;
                </div>
                &lt;?php echo $header; ?&gt;
                <div id="tab_area">
                    &lt;?php echo $tab_area;?&gt;
                </div>
            </div>
            <div id="main">
                <div id="content">
                    &lt;?php echo $content; ?&gt;
                </div>
            </div>
            <div id="footer">
                &lt;?php echo $footer; ?&gt;
            </div>
        </div>
    &lt;/body&gt;
&lt;/html&gt;

#2
[eluser]flaky[/eluser]
Extend the Controller.

Use MY_Controller, put the code that you need repeating in the mentioned Controller.

#3
[eluser]Colin Williams[/eluser]
We really, as a community, need to generate a de facto response to this question that comes up 5 times a day in 600 different flavors. The wiki touches on it in a few places but I don't think it's clear enough. Maybe I'll tackle that tonight or tomorrow.

#4
[eluser]Mithun[/eluser]
@flaky, @Colin Williams Thanks
Got some stuff from here http://codeigniter.com/wiki/Extending_Co...tionality/

But still some confusion, i need to move my common code to MY_Controller? is it right to edit a file in system folder?

#5
[eluser]Colin Williams[/eluser]
No. Don't touch system code. MY_Controller would exist at application/libraries/MY_Controller.php. Most of your global logic will live in MY_Controller's constructor function, typically getting data and making it available to views through $this->load->vars() or by writing it to a template region.

Consider this

Code:
class MY_Controller extends Controller {

   function MY_Controller()
   {
      parent::Controller();
      $data['user_name'] = $this->dx_auth->get_username();
      $this->template->write_view('user_area','user_area', $data);
   }

}

And then be sure your Controllers in application/controllers extend MY_Controller, not just Controller (unless you WANT to bypass what MY_Controller does). That's the gist of it.

#6
[eluser]Mithun[/eluser]
As i don't want each unnecessary extra master page template features of MY_Controller as I already have a robust Template Library, I decided to write my own APP_Controller.php and put it in system folder

Code:
&lt;?php

class APP_Controller extends Controller {
     /**
     * The APP_Controller constructor method.
     */
    function __construct(){
        parent::Controller();
        $this->load->library(array('template', 'DX_Auth'));
        $data ['user_name'] = $this->dx_auth->get_username();
        $this->template->write_view('user_area','user_area', $data);
    }
}

and
Quote:class Examples extends APP_Controller

Thes result was
Code:
Fatal error: Class 'APP_Controller' not found in C:\wamp\www\ci\system\application\controllers\examples.php on line 2

#7
[eluser]Colin Williams[/eluser]
Quote:As i don’t want each unnecessary extra master page template features of MY_Controller as I already have a robust Template Library

Makes no sense. And, how do you suppose your APP_Controller file would load? You didn't follow any of CI's naming and file location conventions.

Quote:I decided to write my own APP_Controller.php and put it in system folder

CI looks in application/libraries for filenames that match those in system/libraries, but prefixed with 'MY_' (a prefix you can change in config if you want) and loads those files automatically. You can keep your APP_Controller in the system folder if you want, but you'll have to include() or require() it manually

#8
[eluser]Mithun[/eluser]
I just got that My APP_Controller is not loading ;-)

I Tried the code

Code:
class MY_Controller extends Controller {

   function MY_Controller()
   {
      parent::Controller();
      $data['user_name'] = $this->dx_auth->get_username();
      $this->template->write_view('user_area','user_area', $data);
   }

}

and

Code:
&lt;?php
class Opportunities extends MY_Controller
{
    function Opportunities()
    {
        parent::Controller();
        $this->load->helper(array('url', 'form'));
        $this->load->library(array('Form_validation','template', 'DX_Auth'));
    }
does not fire the code inside MY_Controller's constructor


But the below combination, code without MY_Controller works
Code:
&lt;?php
class Opportunities extends Controller
{
    function Opportunities()
    {
        parent::Controller();
        $this->load->helper(array('url', 'form'));
        $this->load->library(array('Form_validation','template', 'DX_Auth'));
        $data ['user_name'] = $this->dx_auth->get_username();
        $this->template->write_view('user_area','user_area', $data);
    }

#9
[eluser]flaky[/eluser]
maybe this is the culprit
Code:
&lt;?php
class Opportunities extends MY_Controller
{
    function Opportunities()
    {
        parent::Controller();//this here

change it to
Code:
&lt;?php
class Opportunities extends MY_Controller
{
    function Opportunities()
    {
        parent::MY_Controller();

#10
[eluser]Mithun[/eluser]
yea that was, just changed to
Code:
&lt;?php
class Opportunities extends MY_Controller
{
    function Opportunities()
    {
        parent::__construct();
    }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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