I've built a chat plugin consisting of a chat.php controller, chat_model.php, chat_view.php and chat.js. I'm also using jQuery to perform AJAX requests to the controller.
The chat works fine as its own web page (with a dedicated controller + view), but ultimately I want the chat to function and be accessible in other controllers and able to be viewed in other web pages, in a more independent/modular way. I'm just not sure what the MVC way to structure this would look like.
Should I convert the chat controller to a library so that it can be accessible by other controllers? If so, what will that mean for my AJAX requests? Will I be able to make an AJAX request to a library file or will I still need to retain a (smaller) chat controller to be the middle-man between the those AJAX requests and the library?
If not a library, is there a way to use controllers more modular-ly where they can cooperate and be called by other controllers?
you need to change the chat controller to a library class constructor and model to the class body for that chat library, and autoload that library in your application, then fetch the chat view in the controllers you want to display the chat, and assign it to a variable to use in the current controller's view. like:
class myChat {
public __construct(){
// controller codes here
}
public saveChatMessage($message){
// model codes here
}
}
then for the views,
//in any controller you want to display the chat
$this->load->view("controller.php");
$this->load->view("chat.php");
or
$data["chatview"] = $this->load->view("chat.php","",true);
$this->load->view("controller.php",$data);
and for the backend AJAX responders, you need a controller to access them in URL based format.