• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I choose controllers? and how do I use more than two controllers on a web page?

#1
[eluser]Unknown[/eluser]
Hi!

I am a little new to CodeIgniter and Php. I want to build an app and am wondering how to decide my choice of controllers? I have already designed my database and I have my forms sketched already. I have a problem with controllers as per design and usage.

Do I use just one controller per web page? Or can I use more than one controller per page?
If I can use more than one controller, is it impossible to call them independently of each other without having a full page reload? Is that possible using Ajax? Can you point me to a good tutorial?

Thank you very much.

#2
[eluser]CroNiX[/eluser]
Think of a controller as a subject. Anything having to do with that specific subject gets handled by that controller.

For instance, a blog controller would handle everything to do with the blog. A user controller would handle creating/editing/deleting/etc users, but it wouldn't have anything to do with the blog, except for user permissions, etc.

Typically, if you "need" to use more than one controller at a time, then it isn't really a controller and should be put into a library, where it can be accessed via many controllers. Occasionally, you do need something from another controller and yes, you can do that with ajax. Like I have a user library, which gets used by just about all controllers. Additionally, there is a user controller, which just access the user library.

#3
[eluser]Iszuddin Ismail[/eluser]
Hmmm... I don't know if my explanation is going to give you more headache. Tongue

But a controller will generate the URL. Example, if you have controller Post for handling blog post, and you create functions like edit, add, view, you will get URLs like...

http://localhost/index.php/post/view
http://localhost/index.php/post/edit
http://localhost/index.php/post/add

Just like building any website with PHP, you can have more than one form per page. For example, you can have a form for adding categories on the fly and also a form to add new posts. Both of that is in the add function, for example. But each form, because of the different data, needs a different backend hander, or the action property for the form.

Normally we simply create some $_POST data processing on the top part of the function before processing data that needs to be output. But you can also create separate controller functions to handle different forms in a controller function.

Example

http://localhost/index.php/post/add_post -- to handle add new post
http://localhost/index.php/post/add_cat -- to handle add new category

You just set the right action property in the form, pointing to the right controller function URL.

But you would also need to track where the data is coming from the redirect users back to the right location. Of course this is just an example. I am not saying that this is ideal. But I hope this provides a better understanding what a controller is and what you can do with them.

#4
[eluser]Aken[/eluser]
Reading the CI manual and doing a couple tutorials will help you learn these things. Start there.

Although in my opinion, you should learn more about PHP by itself before trying to understand a framework.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.