Welcome Guest, Not a member yet? Register   Sign In
Building application structure - problems with controllers?

Hello everyone

I am new here and I am also new to CI, so please forgive me if my question makes no sense. I will explain it based on an example:

I would like to have template - the main index.php of the template that would include some custom tags which would be later on parsed by my parser (kind of a simplified joomla templating). These tags would get replaced by different views as a part of the whole page. My question now is, if I have, for example, a template, that contains the main view (content of my website) and, let's say, login window, what happens if I want to log in and the form action is a controller of this login window which was not loaded (this is what I suppose, so will it load it automatically or throw an error?), because the whole webpage was loaded by the main view's controller. Would You suggest to go for a template controller that would be main controller that loads rest of controllers needed (main view's controller, login window's controller, etc.)?? How would You perform such controllers loading? Is there a dedicated function or should it be a helper function written myself?

Thanks in advance for any effort You guys will put in solving my problems.

Kind Regards

I would create a library to do that, which you can autoload and use from any controller.

Well, and what would that library do? Load additional controllers? How can these be loaded, using a function dedicated for this?? I am getting it right?

Library loaded via autoload -> parsing template -> loading appropriate controllers based on what the template contains?

If yes, then there is this problem: In the time autoload is performed we don't know which template is going to be used and what modules should be included, this will be known only after initializing the first controller that will select the appropriate view (template) to be loaded.

You can't "load" controllers. They get automatically loaded depending on the URL, or route (if used), which is how CI works. You "autoload" the library, which the controllers can then use.

In your controller, you pass data to your template library and it does the manipulation and outputs the final views, depending on what you need.

I'd read the user guide on the differences between libraries and controllers.

Right, I do understand this, but I think You may have misunderstood my concern. I have a problem, because my login window view is managed by a different controller than my, let's say, news page. This means that, if I load my home page with news and put a login form on (load my login window view) that points to, lets say, login controller, this means I cannot use this controller's methods, since it has not been loaded - can't launch the login method of this controller. This is the problem.

Why would you do it that way? The problem is self-caused.

When someone logs in, you should store their data in session and then redirect back to where they were before logging in, or a profile page or something. Now they are logged in and you can display different info based on their userlevel, or however you have it set up. It's a new request (because of the redirect), so you would be able to use any methods in your home controller.

So, they are on the homepage and log in, which sends their form data to the login controller. Login Controller saves their user information in session and then redirects them back to the homepage where they were when they logged in. Now they are on the same page as they were before, but logged in (in which case you wouldn't display the login form anymore, kind of like how it is on this site.)

Well, that's probably I don't really like the redirection way of doing it and also because I would have few more "modules" included, that actually have to display some data WITHIN the template. For example a little slideshow on the website with the option to slide images (therefore has to access its own controller to load new image), this can't be placed on my content view, because it is going to be displayed on every single view (this is why it's on the template. There would be much more examples of what I want to achieve, so I would like to ask if there is any alternative way to do what You said in the way I would like it to work.

Use a MY_Controller and extend all your other controllers from it.

Well, could You elaborate it a bit? Do you mean I should create a controller My_Controller (the one that extends CI_Controller and is autoloaded) that handles ALL requests of my template? Cause this is what it is going to do in this case, since I don't know what "module" of the template is going to be loaded (different view means different modules placed on the template based on the database). I will maybe "draw" it step by step:

Application initializes
Application processes the URL to request right controller
Application loads appropriate controller
Controller processes the request using its methods and its model and takes the template along with data passed to it
Custom parsing library is loaded
Parser parse the template to prepare final page (containing "content" view and "module" views)
Controller displays the page (probably using set_output and other required functions)
Now - how do I handle different "module" controllers? I would like, for example, have there AJAX based module of something, this means it needs ITS controller to: get the request, call its module to process data/gather data, send the reply to the script. Before the template is parsed I don't know what "module" views will be loaded, therefore I don't know which controllers' functionality I may need.

Should everything be put in one controller having ALL the functionality for the whole website, just loading different variants of the template and controlling the whole application?

CodeIgniter Base Classes: Keeping it DRY

Theme © iAndrew 2016 - Forum software by © MyBB