Welcome Guest, Not a member yet? Register   Sign In
Best Practice questions (headers, footers, sessions, oh my!)
#1

[eluser]tjans[/eluser]
I'm new to the framework, and so far it's been very easy to get up and running. I'm sure I'm never going back to rolling my own stuff! That being said, I have some questions.

1.) If I have a header and footer view that I want every other view to be contained within, do I have to load the header, content, and footer view in every controller that uses it, just as I would include the header and footer in my content on a normal php page? Or, is there some cleaner way to not have to repeat the inclusion of header/footer views in every controller method?

2.) Let's say that when I'm logged in, I have a view that displays "Welcome $username" and when not, "welcome - please login." since the username is a variable that needs to get set in the controller, I'd have to have every controller that uses this view actually pass in the username...is there a cleaner way to do that then actually having to duplicate the code that sets the username variable and then renders the header view?

3.) For user authentication, I'm used to using the global php vars such as $_SESSION['UserID'] to determine if a user is logged in...what is the recommended pattern to use in CI? The built-in session stuff? Is there a better option?

Thanks in advance. The documentation for CI is really good, and this was the most painless framework to get rolling on out of the box. I'm sold...
#2

[eluser]dkrape[/eluser]
On 2 and 3, I'm using Tank Auth and it adds this sort of information to the session. In the view I can get this information by using: $this->session->userdata('user_id');

Tank Auth uses the following code to log the user_id and user_name into the session:

Code:
$this->ci->session->set_userdata(array(
  'user_id'    => $user->id,
  'username'    => $user->username,
  'status'    => ($user->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED,
));
#3

[eluser]tjans[/eluser]
I've thought about using tank auth, but I wanted to write a simple one on my own first for educational purposes...thanks for the reply...
#4

[eluser]dkrape[/eluser]
Yeah, I did that at first, but found it was just quicker to use tank.

Regarding 1, check out this thread: http://ellislab.com/forums/viewthread/173268/ I'm something of a CI newbie so I haven't tried the templating engine. It looks interesting though.
#5

[eluser]tjans[/eluser]
I'll check it out...though I'll likely stay away from the templating engine for now. I enjoyed smarty, but I'm trying to stay raw for now so I can learn how everything works. THanks again, brother dkrape...
#6

[eluser]Dennis Rasmussen[/eluser]
Hello there tjans,

1) There are multiple good ways of achieving this without much hassle. The first example would be to sub-include view files within view files (just like you did with native php, header/footer included in the html file). Another option is to use a template library (mainly one of those built for CI, there are several and they do it in different ways). One template engine (library) uses the controller to setup the header and footer for each page/method, other template engines use yet another method which involves in having a master page (master layout file) and then you add in data to be used in this layout file (selected layout can be changed independently) and a lot of other ways.

2) For data which you need everywhere on the site you can create your own custom library which you autoload or you can create an extension to the CodeIgniter controller which you then extend from instead in every controller you have (known as MY_Controller, read userguide)

3) if ($this->session->get_userdata('id')) {}
returns false if id isn't set.


Hope this helps Smile
#7

[eluser]tjans[/eluser]
Thanks for the reply. I'll look into your suggestions, they sound reasonable and close to what I thought I'd have to do, but I wanted I ask first to make sure I didn't reinvent the wheel or take the most difficult path when there was an easier one. CI has given me new inspiration to code. Thanks...
#8

[eluser]mihaibaboi[/eluser]
I don't know about 2. and 3. but I think I can help you with the templating system. I use an external library that I like very much. It's very clean and easy to set up. It's called Template Library for CodeIgniter and I've been using it for a while. Give it a try.




Theme © iAndrew 2016 - Forum software by © MyBB