问题描述
我刚刚开始使用CodeIgniter,我试图哈希我的常规模块/函数,以使它们在MVC框架内正常工作。对于具有强大CodeIgniter背景的任何人,我有几个具体问题:
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:
SESSIONS
CodeIgniter会话将客户端的会话数据存储在cookie中,这对我来说不起作用。我知道有一些替代品,或者我可以建立自己的库/帮手;但我只是看不到任何好处,只是使用 $ _ SESSION
。
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
.
如果我只是使用 $ _ SESSION
,我会有其他框架的任何问题吗?框架的任何其他部分是否依赖于使用CodeIgniter会话?
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?
我觉得有点奇怪的走出框架外的东西这么基础,但我很舒服纯PHP。我基本上只是想使用CodeIgniter for MVC,并为我的项目强制一个更模块化的方面。
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.
CODE FLOW& CONFIG
我有几个配置项需要在几乎任何其他事情之前完成。
I have a few config items that need to be done before almost anything else.
例如,假设我有一个常数 APP_LIVE
,根据当前服务器。这必须发生真的很早,路径,错误报告,CodeIgniter系统和应用程序文件夹等将根据它设置。
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.
问题是, code> system_folder 和 application_folder
(将根据运行代码的服务器设置) index.php
文件,在任何配置加载之前。
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.
网址中的内容,并且可能会在网页加载之前重定向。例如,一些页面需要在URL中存在 www。
(用于SEO),跟踪附属机构,访问者来源,营销标记等。
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.
哪里是最好的地方,这样的事情,必须发生真的早?我知道有一个配置文件,一个自动加载文件,一个常量文件等,但是对于一些项目来说太迟了。将这些东西放到主要的 index.php
文件的顶部,或者做一个包含全局配置文件的文件,这是不好的做法吗?再次,我觉得我走出框架,想知道我是否只是这样做,因为我还没有一个坚实的理解呢?
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?
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.?
推荐答案
新发布的CI 1.7处理数据库中的会话(如果您使用的话)。
The newly released CI 1.7 handles sessions in the database (if you're using one).
但是,CI设计为松散耦合,因此如果您决定使用$ _SESSION,则不应注意到任何主要问题。
However, CI is designed to be loosely coupled, so you shouldn't notice any major issues if you decide to use $_SESSION instead.
对于您的页眉/页脚/导航,您可以创建(例如)headerview.php,footerview.php和contentview.php,并通过在控制器中执行类似操作将数据传递到您的视图:
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.
我已经添加了index.php一次或两次,设置初始值等,从来没有遇到过问题。
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. ;)
这篇关于CodeIgniter问题:本地PHP会话,代码流,布局问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!