CodeIgniter問題:本機PHP會話,代碼流,布局問題?

[英]CodeIgniter questions: native PHP sessions, code flow, layout issues?


I am just getting started with CodeIgniter, and I am trying to hash out my regular modules/functions to get them working properly within the MVC framework. I have a few specific questions for anyone who has a strong CodeIgniter background:

我剛剛開始使用CodeIgniter,我正在嘗試散列我的常規模塊/函數,以使它們在MVC框架內正常工作。對於擁有強大CodeIgniter背景的人,我有幾個具體問題:

SESSIONS

The CodeIgniter 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.

CodeIgniter會話將客戶端的會話數據存儲在cookie中,這對我來說不起作用。我知道它有一些替代品,或者我可以建立自己的庫/助手;但我認為只使用$ _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 CodeIgniter session?

如果我只使用$ _SESSION,我會對框架的其余部分有任何問題嗎?框架的任何其他部分是否依賴於使用CodeIgniter會話?

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 CodeIgniter for MVC, and to enforce a more modular aspect for my projects.

我覺得有點奇怪的是走出框架以外的東西這么基本,但我對普通的PHP非常熟悉。我基本上只是想將CodeIgniter用於MVC,並為我的項目強制實現更模塊化的方面。

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 CodeIgniter system, and application folders, etc. will be set based on it.

例如,假設我有一個常量APP_LIVE,它根據當前服務器的名稱設置為true / false。這必須很早發生,因為路徑,錯誤報告,CodeIgniter系統和應用程序文件夾等將基於它進行設置。

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.php file, before any of the configs have loaded.

問題是system_folder和application_folder(將根據運行代碼的服務器設置)首先在index.php文件中設置,然后才能加載任何配置。

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.

此外,我有一個功能,可以檢查URL中的內容,並可能在頁面加載之前重定向。例如,某些頁面需要存在www。在URL(用於SEO),跟蹤附屬機構,訪客來源,營銷標志等。

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.php 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?

把這樣的事情放在哪里的最佳地方必須在哪里發生?我知道有一個配置文件,一個自動加載文件,一個常量文件等,但對於某些項目來說已經太晚了。簡單地將這些東西放在主index.php文件的頂部,或者將其包含在全局配置文件中是不是一種壞習慣?再一次,我覺得自己正在走出框架,並想知道我是否只是這樣做,因為我對它還沒有充分的了解嗎?

LAYOUT / HEADER FOOTER

布局/標題腳

Like most people, I have a top header, navigation, footer, etc. I am used to just having them in 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.?

像大多數人一樣,我有一個頂部標題,導航,頁腳等。我習慣於將它們放在文件中,這些文件包含在我的頁面模板中。我相信我可以通過創建視圖並將它們包含在我的主頁面視圖中來以相同的方式執行此操作。這是最好的方式嗎?其中一些需要一些數據;喜歡它們用於導航等的頁面。處理導航,共享頁眉/頁腳等的最佳方法是什么?

5 个解决方案

#1


3  

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

新發布的CI 1.7處理數據庫中的會話(如果您使用的話)。

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

但是,CI設計為松散耦合,因此如果您決定使用$ _SESSION,則不應注意任何重大問題。

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:

對於頁眉/頁腳/導航,您可以創建(例如)headerview.php,footerview.php和contentview.php,並通過在控制器中執行以下操作將數據傳遞到您的視圖:

$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.

我自己已經為index.php添加了一兩次,以設置初始值等,並且從未遇到過問題。

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

祝賀您選擇框架;我相信你不會失望的。 ;)

#2


1  

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).

你可以在每個控制器中有多個load-> view行,但我個人覺得它是耦合的。我強烈建議您查看CodeIgniter中的鈎子,您可以在其中定義在每個控制器/方法之后自動運行的函數(AOP的一個很好的例子)。

#3


1  

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/

實際上$ _SESSION數組似乎未設置,因此您無法使用本機PHP會話(至少在1.7上)。但是在CodeIgniter wiki中有一個使用本機php會話的會話類 - 你可以像使用其他方式一樣使用它,但它只在cookie中存儲session_id。這是:http://codeigniter.com/wiki/Native_session/

#4


0  

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

@lacho我在$ _SESSION上創建了自己的auth庫。它在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.

我認為$ _SESSION更加安全,因為CI'會話'是存儲在客戶端的cookie,被歸類為不可信任的“用戶傳遞信息”。

#5


0  

You can try with native using your own session class

您可以使用自己的會話類嘗試使用native

http://www.moreofless.co.uk/using-native-php-sessions-with-codeigniter/


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/11/08/72576ba9509fd299397d280741863738.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com