I am just getting started with this framework, and I’m fairly new to the MVC concept. Right now I am following the tutorial on the official documentation and my controller looks like this:
class Pages extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
public function goto($page)
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
I have also a home.php file with the homepage:
<?php $this->load->helper('url');?>
<div>
<p>This is the home page</p>
</div>
<a href="<?php echo base_url()./index.php/pages/goto/about;?>">About</a>
<div id="content">
<form method="post" action="">
<div>
<label for="username">Username:</label>
<input type="text" name="username"/>
</div>
<div>
<label for="password">Password:</label>
<input type="password" name="password"/>
</div>
<div>
<input type="submit" value="Login"/>
</div>
</form>
</div>
Until now everything works, but how can I add and work with new links? I mean, suppose that you have a list of links in the homepage; when the user clicks on one of those links, how am I supposed to handle the requests? Should I let them go through the “pages” controller? Should I create a new controller for every page of the website?
In the first case (which to me sounds more logical), how can I correctly redirect the user based on the link they clicked on?
Any help would be appreciated.
Standard way is
controller/methods/but ofcourse you can use router. Router infoDepends. Is those
pagesare related to controller? if yes, then include. if no than don’t. Point is, bring together the related contents under one controller. You won’t want say admin functions inpagecontroller. do you?Simple answer. No. Reason is above.
again the 1st point.