views:

3402

answers:

5

I am just getting stated with CodeIgniter and am trying to hash out my regular modules/functions and get them working properly within the mvc framework.

I have a few specific questions, if anyone has a strong background with CI:

SESSIONS

The CI session stores session data on the client side in a cookie, which just isn't going to work for me. I know there are a few replacements for it, or I could build my own library/helper, but I just don't see any benefit over just using $_SESSION.

If I just use $_SESSION, will I have any problems with the rest of the framework? Does any other part of the framework depend on using the CI session?

I feel a bit weird about stepping outside the framework for something so basic, but I am pretty comfortable with plain PHP - I am basically just looking to use CI for mvc and to enforce a more modular aspect for my projects.

CODE FLOW & CONFIG

I have a few config items that need to be done before almost anything else.

For example, say I have a constant APP_LIVE, which is set true/false based on the name of the current server. This has to happen really early, as paths, error reporting, the CI system and application folders, etc, will be set based on it.

The problem is that the system_folder and application_folder (which will be set based on which server the code is running on) are set first thing in the index file, before any of the configs have loaded.

Also, I have a functions that check for things in the url, and may redirect before the page ever loads. For example, some pages need to enfore the presence of www. in the url (for seo), track affiliates, visitor sources, marketing flags, etc.

Where is the best place to put things like this that have to happen really early? I know there is a config file, an autoload file, a constants file, etc, but those are too late for some items.

Is it a bad practice to simply put these things into the top of the main index file, or to make an include there to a global config file?

Again, I feel like I am stepping outside the framework, and wonder if I'm just doing that because I don't have a solid understanding of it yet?

LAYOUT / HEADER FOOTER

Like most people, I have a top header, navigation, footer etc.

I am used to just having them in include files, which are included into my page template. I believe I can do that the same way, by just making them views and including them into my main page view. Is that the best way to go? Some of them need a bit of data, like what page they are on for the navigation, etc. What's the best way to handle navigation, shared header/footer, etc?

+3  A: 

The newly released CI 1.7 handles sessions in the database (if you're using one).

However, CI is designed to be loosely coupled, so you shouldn't notice any major issues if you decide to use $_SESSION instead.

For your header / footer / navigation, you could create (for example) headerview.php, footerview.php, and contentview.php, and pass data to your views by doing something like this in the controller:

$data['title'] = 'about us';
$data['content'] = 'hello world!';

$this->load->view('headerview', $data);
$this->load->view('contentview', $data);
$this->load->view('footerview');

Basically, you can treat these views exactly like includes, but with the added benefit that you can change the variables within. I would steer clear of calling other views from within views, but that might just be me.

I've made additions to index.php myself once or twice, to set initial values and such, and have never had a problem with it.

Congratulations on your choice of framework; I'm sure you won't be disappointed. ;)

John McCollum
+1  A: 

You can either have multiple load->view lines in every controller but I personally find it coupled. I strongly suggest that you take a look at hooks in CodeIgniter where you can define functions that would be automatically run after each controller/method (a fine example of AOP).

Mehmet Duran
+1  A: 

Actually the $_SESSION array seems to get unset so you can't use the native PHP sessions (at least on 1.7). However in CodeIgniter wiki there's a session class that uses the native php sessions - you can use it the same way as the other, but it stores only session_id in the cookie. Here it is: http://codeigniter.com/wiki/Native_session/

Hi - Thanks for that. Are you sure? The $_SESSION seems to be working ok on the development server, but I'd hate to see it suddenly vanish one day. We are using 1.7.0.
Eli
Hi, actually I just started using CI and I'm not really sure in anything :P Could be that I'm missing some config setting ot something... But i'm also using the implementation from the link and so far am pretty happy - also supports the flashdata feature. I think it's worth checking. Good luck!
Thanks! (here are some more characters, since 10 are required)
Eli
A: 

@lacho I created my own auth library on $_SESSION. and it works fine on 1.7.

I believe $_SESSION is much more secure since CI 'sessions' are cookies that are stored on the client side which are classified as 'user-passed-information' that can't be trusted.

Thorpe Obazee