To better help you, I first need to details bit more how MVC works. I've never worked with CodeIgniter, but from experience with Laravel and other MVC frameworks, Model-View-Controller (MVC) design paradigm organizes your application into three main domains - the model, which usually represents the database entities and other types of encapsulated logic, the view, which generates the final output (often HTML) that the user receives, and the controller, which controls the flow of interaction between the model and the view (and may contain some logic of its own as well).
Additionally, you'll have a front controller handling all calls to your application. In this case, this is your index.php
file. This front controller uses routes to map various endpoints. This is how your application links urls to controller methods.
All this means a single route (url) can typically calls only one controller method.
Now, for your specific question, it's only a matter or organizing your code so common code can be shared by multiple controller methods. You can have shared protected methods inside your controller class which can be called by both routes.
For example, your controller class can be structured like this :
public function firstPage()
{
$foo = $this->getBar();
// Load first page view
}
public function secondPage()
{
$foo = $this->getBar();
// Load second page view
}
protected function getBar()
{
return 'Bar';
}
In this simple example, you can use the getBar
method to load all data or perfrom CRUD operation shared by your two pages / controller methods, firstPage
and secondPage
.
In short, keep in mind a controller class and its method should be as simple and short as possible. I recommend sending more complex logic to services or other classes. For example :
protected function getBar()
{
return new Bar();
}
In this case, it will be easier to Extend the Bar
class than extending a controller class or method, if you require so. It will also help you, in the long run, when dealing with UnitTesting.