Welcome Guest, Not a member yet? Register   Sign In
Keeping DRY
#1

[eluser]TriCityGuy[/eluser]
I'm sure I'm overlooking something completely obvious. I'm pretty new to MVC. Although I'm loving it, still a few "best practices and procedures" I'm trying to grasp.

I've condensed my code down to simply illustrate what I'm looking for. In a more simplified block of code I have this:

Code:
$data['content'] = "login_view";
$data['logged_in'] = $this->session->userdata('logged_in');
$this->load->view('template', $data);

I'd like to pass in
Code:
$data['logged_in'] = $this->session->userdata('logged_in');
to every view. I've tried passing it in the constructor..

Code:
function Dashboard() {

        parent::Controller();

    }

I could simply pass the $data['logged_in'] line into every controller function I write but it seems like there has to be a better way. A way I'm sure is staring me right in the face. Smile
#2

[eluser]Frank Rocco[/eluser]
I would use a template system like:
Code:
http://www.williamsconcepts.com/ci/codeigniter/libraries/template/?v141

and add the code to the header section.
#3

[eluser]sketchynix[/eluser]
You dont need to pass it in, it is already available
Code:
echo $this->session->userdata('logged_in');
#4

[eluser]TriCityGuy[/eluser]
I know its available with:

Code:
echo $this->session->userdata('logged_in');

I was just trying to clean up a lot of the long form approaches in the view.

Instead of saying:

Code:
if($this->session->userdata('logged_in'))  {

//if true

}

I was just hoping to easily pass $logged_in for all the checks. Just cleaner that way. This will do though. Thanks for the help!
#5

[eluser]techgnome[/eluser]
I put mine into a library... then the library is autoloaded... in the view I can then do
Code:
if($this->myLib->am_I_logged_in()) { echo "I'm Loggin!"; } else {echo "Youuuu shall not PASSS!"; }

Actually in reality, I've actually factored even that logic out into my library method so that conditional isn't in the view at all:

Code:
//In the library

function get_admin_link() {
  if($this->am_I_logged_in() && $this->am_I_admin()) {
     return anchor('site/admin');
  } else {
    return ""; // Not logged in and not an admin -- no link
  }

.
.
.
// In the View:
<?php echo $this->MyLib->get_admin_link(); ?>

Now my view doesn't care how I get the login info... even better, if I decide later I want to change how I validate the user (which I have actually done three times already), I update the library - the view remains unchanged and unaware of the change.

-tg
#6

[eluser]Frank Rocco[/eluser]
I thought the OP did not want to have to code this in every view, that is why I suggested the template system and using a header view.
#7

[eluser]TriCityGuy[/eluser]
[quote author="techgnome" date="1287443857"]I put mine into a library... then the library is autoloaded... in the view I can then do
Code:
if($this->myLib->am_I_logged_in()) { echo "I'm Loggin!"; } else {echo "Youuuu shall not PASSS!"; }

Actually in reality, I've actually factored even that logic out into my library method so that conditional isn't in the view at all:

Code:
//In the library

function get_admin_link() {
  if($this->am_I_logged_in() && $this->am_I_admin()) {
     return anchor('site/admin');
  } else {
    return ""; // Not logged in and not an admin -- no link
  }

.
.
.
// In the View:
<?php echo $this->MyLib->get_admin_link(); ?>

Now my view doesn't care how I get the login info... even better, if I decide later I want to change how I validate the user (which I have actually done three times already), I update the library - the view remains unchanged and unaware of the change.

-tg[/quote]

This is right along the lines of what I was looking for. Thanks!
#8

[eluser]bretticus[/eluser]
You can look for a library, but I just make this extraordinarily simple with your original controller constructor idea. I just use $this->load->vars(). I mean, it's nice to reference $is_logged_in or $admin_link in my views instead of $this->MyLib->get_admin_link(). It's personal preference really. I just don't like seeing $this anywhere in my views (as they are often styled by non-programmers.) To overcome the "every controller" scenario, it's easy to just stick this in a MY_Controller constructor. One nice thing about this is that I don't have to worry about whether an auth library has been loaded for a given controller. I can even extend Controller with two or more "sub controllers" that use an auth lib...or not. Just my preference.




Theme © iAndrew 2016 - Forum software by © MyBB