Welcome Guest, Not a member yet? Register   Sign In
Redirect vs load view
#4

(09-17-2018, 06:59 AM)Serializable Wrote: Do you mean `FOR all classes`?

But how many times that controller creates? 
Every time I request it or it creates only once and then CI reuses it?

When it comes to controllers - 'create' might be confusing term to use.

Controllers are built with PHP classes, but they are only 'loaded' once per page request. If you have multiple methods in one controller (all the different functions), sometimes you might want to apply some checks for every method in this controller - by putting the check code in __construct, you can cut down on amount of work you have to do. In your example it checks if user is logged in, so __construct and redirect are probably most common ways to stop not-logged-in users seeing things they shouldn't.

For redirect v view, this also helps you better structure your code.

Lets say there's a specific list, lets say latest news, you need to load on your home page. Instead of saying, if user has not logged in, I now have to remember to load in this list, you can just send users browser to home page controller via redirect, it enforces users browser to load a specific URL. It creates another request, but also you only have one place that you'd have to handle that specific list of items.
Reply


Messages In This Thread
Redirect vs load view - by Serializable - 09-16-2018, 10:22 AM
RE: Redirect vs load view - by InsiteFX - 09-17-2018, 03:45 AM
RE: Redirect vs load view - by Serializable - 09-17-2018, 06:59 AM
RE: Redirect vs load view - by Pertti - 09-17-2018, 08:00 AM
RE: Redirect vs load view - by Wouter60 - 09-17-2018, 08:37 AM
RE: Redirect vs load view - by dave friend - 09-17-2018, 09:16 AM
RE: Redirect vs load view - by Serializable - 09-17-2018, 12:25 PM
RE: Redirect vs load view - by dave friend - 09-17-2018, 04:43 PM
RE: Redirect vs load view - by ignitedcms - 09-21-2018, 11:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB