Welcome Guest, Not a member yet? Register   Sign In
Confused about variables
#5

The questions aren't dumb, they just show an underlying assumption that is hard to let go of when moving from desktop programming to web programming (something I did myself a little over 10 years ago). Further, there are some differences between languages and environments that can make some web programming experiences more like desktop programming (ASP.Net and C# can blur the lines enough to make it easy to transition, but difficult to get a good picture of what's really happening). I do think you're on the right track, though. I apologize for the length of this in advance, and please feel free to ask if I missed something by not replying to your message point-by-point, because I felt it was easier to explain things in a different order.

In the normal case for PHP, a user follows a link, types in a URL, submits a form, or in some other way generates a Request, which, in the case of CodeIgniter (and many other PHP frameworks) is sent to a single script, index.php. That script usually handles the Request by instantiating a Controller and calling a method on that Controller instance. The Controller might load any number of Libraries and Models to deal with the Request, but, in the end, it usually loads one or more Views. When the Controller method called by the script ends, execution returns to the CodeIgniter script, which usually processes the View(s) and generates a Response, which is usually the page sent back to the user. At this point, the script is done, and execution ends. See The user guide's Application Flow Chart.

Your Controller instance terminates with the script once the Response is sent to the user, and anything stored in the Controller's properties is lost along with it.

So, if you want data to be maintained between Requests, you have to save it somewhere. Sessions allow you to setup a temporary storage which attempts to stick with the user between Requests (in PHP, a Cookie is almost always used to associate a Session with a user, but CodeIgniter does not store the actual Session data in the session Cookie, it stores it on the server according to the application's configuration). Sessions can be problematic for certain situations, and are never appropriate for data which requires long-term availability or needs to be available to multiple users. For those instances, you have the options of using a Database or writing to a file (caching is also an option, but is usually used more to speed up a site than for data storage; I should also add that CodeIgniter has a Caching Library which supplies wrappers for the most popular PHP Caches, but it also supports Output/Web Page Caching, Database Caching, and Query Builder Caching, all of which do very different things and are a source of endless confusion).

So, setting up a link in a View is fine, and it will direct a user back to your Controller. However, when the user clicks on a link on a web page, it is generating a completely new Request which is handled by a completely new instance of your Controller. It doesn't matter whether the page which contains the link was generated by the same Controller. It doesn't even matter if the user is submitting a form which posts the form data back to the same URL.

Now, if you just need to pass data to a method in the Controller, and it isn't important that the data is hidden from the user (and the data doesn't need to be shared between users), you can store it in the page. If the user calls the Controller method by clicking a link, then you would most likely store it directly in the URL which that link calls. If you're passing data to the Controller from a form, you might add hidden fields (<input type='hidden' name='something' value='your data here' />) to your form or put the data into the appropriate types of controls, then Post the form using a Submit button, at which point the Controller would access the data via $this->input->post(). Remember that the user can still choose to use a debugger or "View Source" in their browser to see whatever data you've put into hidden fields, and a debugger or other tools may even allow them to change the data. There are potentially a few other ways to pass data from a page back to a Controller, but these are the most common.

Something else I meant to mention earlier is that the Controller instance in CodeIgniter is a Singleton, and the Loader, along with most of the classes loaded by the Loader, gets attached to the Controller as properties. This is why you can access the Loader via $this->load->model()/library()/view()/etc., and why your loaded model becomes available via $this->model_name, or a library via $this->library_name(). This is also why I warn against using a lot of properties in the Controller, as you eventually run the risk of name collisions between properties and the loaded classes. Additionally, Models will search the Controller instance for any properties or methods which can't be found on the Model itself (allowing the model to access any currently-loaded libraries/models just as you would in the Controller). Finally, any public methods in the Controller which do not start with an underscore (_) can be routed, so they will usually be accessible via a URL (and, in PHP, any method or property which does not have its visibility declared is public).
Reply


Messages In This Thread
Confused about variables - by MightBeABitLate - 07-27-2016, 03:35 PM
RE: Confused about variables - by InsiteFX - 07-28-2016, 04:28 AM
RE: Confused about variables - by mwhitney - 07-28-2016, 10:24 AM
RE: Confused about variables - by MightBeABitLate - 07-29-2016, 05:06 AM
RE: Confused about variables - by mwhitney - 07-29-2016, 07:26 AM
RE: Confused about variables - by dave friend - 07-29-2016, 08:27 AM
RE: Confused about variables - by MightBeABitLate - 07-29-2016, 08:44 AM
RE: Confused about variables - by mwhitney - 07-29-2016, 09:53 AM
RE: Confused about variables - by dave friend - 07-29-2016, 09:05 AM
RE: Confused about variables - by PaulD - 07-29-2016, 09:11 AM
RE: Confused about variables - by MightBeABitLate - 07-29-2016, 10:42 AM
RE: Confused about variables - by dave friend - 07-29-2016, 11:28 AM
RE: Confused about variables - by MightBeABitLate - 07-29-2016, 11:54 AM
RE: Confused about variables - by mwhitney - 07-29-2016, 12:17 PM
RE: Confused about variables - by fmertins - 07-29-2016, 01:14 PM
RE: Confused about variables - by mwhitney - 07-29-2016, 02:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB