I am coding my first CodeIgniter application (very familiar with PHP, but not CI) and have the following setup:
我正在编写我的第一个CodeIgniter应用程序(非常熟悉PHP,但不熟悉CI)并进行以下设置:
I have a controller, Signup, that controls a signup process. Every function of the controller is the next step in the process. I have an object, Did
, that I am currently loading as a library. This object's properties/variables are updated as the signup process moves along.
我有一个控制器,Signup,它控制一个注册过程。控制器的每个功能都是该过程的下一步。我有一个对象,是的,我目前正在加载为库。注册过程随之更新此对象的属性/变量。
The issue I'm having is that the properties from one function of the Signup controller do not carry over to the next function. It is as if the class is re-constructed with every function.
我遇到的问题是Signup控制器的一个函数的属性不会转移到下一个函数。就好像每个函数都重构了类。
Is there a way to reuse the class throughout the controller without it having to be re-instantiated? I'd rather not have to serialize and store in a session, either.
有没有办法在整个控制器中重用该类而不必重新实例化?我不想在会话中序列化和存储。
Thanks in advance.
提前致谢。
2 个解决方案
#1
2
As always, there are many solutions to the same problem. Please disregard this if it doesn't fit well with your implementation.
与往常一样,同一问题有很多解决方案。如果它不适合您的实施,请忽略它。
Keeping the signup steps in an object is a good idea- however, every time you load a new page CI rebuilds all the objects. In order for data to persist it should be stored in the session, but that doesn't mean you have to be working with session variables in your controller.
保持对象中的注册步骤是个好主意 - 但是,每次加载新页面时,CI都会重建所有对象。为了使数据持久存在,它应该存储在会话中,但这并不意味着您必须在控制器中使用会话变量。
How are you transferring data to your application? Is it via forms or ajax?
您如何将数据传输到您的应用程序?是通过表格还是ajax?
One way you can do it is by unserializing the object from the session and storing it as an object in your controller's constructor. That way you can still run $myObj->function() against it and use a $myObj->save() function to reserialize and store it.
一种方法是通过从会话中反序列化对象并将其作为对象存储在控制器的构造函数中。这样你仍然可以对它运行$ myObj-> function()并使用$ myObj-> save()函数来重新序列化并存储它。
Hope that helps!
希望有所帮助!
#2
2
The problem you are having is that you are depending on the in-memory state of your application to remain from request to request.
您遇到的问题是您依赖于应用程序的内存中状态,以便从请求到请求保留。
You're expecting your class to use the same instantiation of your Did
object between requests.
您期望您的类在请求之间使用相同的Did对象实例化。
This is not how PHP/HTTP works. Each request is handled individually and is it's own instance of your application. So each request creates a new Did
object.
这不是PHP / HTTP的工作方式。每个请求都是单独处理的,它是您自己的应用程序实例。因此每个请求都会创建一个新的Did对象。
To persist the state you need to either use Sessions to carry information between requests, use a database to handle your persistent state, or a combination of both.
要保持状态,您需要使用Sessions在请求之间传递信息,使用数据库来处理持久状态,或两者的组合。
Codeigniter数据库类
#1
2
As always, there are many solutions to the same problem. Please disregard this if it doesn't fit well with your implementation.
与往常一样,同一问题有很多解决方案。如果它不适合您的实施,请忽略它。
Keeping the signup steps in an object is a good idea- however, every time you load a new page CI rebuilds all the objects. In order for data to persist it should be stored in the session, but that doesn't mean you have to be working with session variables in your controller.
保持对象中的注册步骤是个好主意 - 但是,每次加载新页面时,CI都会重建所有对象。为了使数据持久存在,它应该存储在会话中,但这并不意味着您必须在控制器中使用会话变量。
How are you transferring data to your application? Is it via forms or ajax?
您如何将数据传输到您的应用程序?是通过表格还是ajax?
One way you can do it is by unserializing the object from the session and storing it as an object in your controller's constructor. That way you can still run $myObj->function() against it and use a $myObj->save() function to reserialize and store it.
一种方法是通过从会话中反序列化对象并将其作为对象存储在控制器的构造函数中。这样你仍然可以对它运行$ myObj-> function()并使用$ myObj-> save()函数来重新序列化并存储它。
Hope that helps!
希望有所帮助!
#2
2
The problem you are having is that you are depending on the in-memory state of your application to remain from request to request.
您遇到的问题是您依赖于应用程序的内存中状态,以便从请求到请求保留。
You're expecting your class to use the same instantiation of your Did
object between requests.
您期望您的类在请求之间使用相同的Did对象实例化。
This is not how PHP/HTTP works. Each request is handled individually and is it's own instance of your application. So each request creates a new Did
object.
这不是PHP / HTTP的工作方式。每个请求都是单独处理的,它是您自己的应用程序实例。因此每个请求都会创建一个新的Did对象。
To persist the state you need to either use Sessions to carry information between requests, use a database to handle your persistent state, or a combination of both.
要保持状态,您需要使用Sessions在请求之间传递信息,使用数据库来处理持久状态,或两者的组合。
Codeigniter数据库类