Welcome Guest, Not a member yet? Register   Sign In
Detailed Explanation of a Controller & Model?
#4

(09-01-2015, 03:38 AM)Josh1985 Wrote: Thank you for your helpful and lengthy post. I am however, still somewhat confused. Please pardon me while I get some clarification on some things.

First the view files are located in:

-------------------------------------------------------
/application/views/
-------------------------------------------------------

And are the following:
-------------------------------------------------------
head.php
header.php
topnav.php
sidenav.php
footer.php
xampp.php
template.php
-------------------------------------------------------

Yes, althought xampp.php should be in a sub-directory named after your controller (so, in my example, /application/views/Something/xampp.php).

Quote:The controller file should be located in:

-------------------------------------------------------
/application/controllers/
-------------------------------------------------------
 
and is this file:
-------------------------------------------------------
MY_Controller.php
-------------------------------------------------------

This should be /application/core/MY_Controller.php, because we're extending the CI_Controller (/system/core/Controller.php).

Quote:Assuming I am correct, I have heard that usually the template, head, header, and footer view files go in:

-------------------------------------------------------
/application/views/includes
-------------------------------------------------------
is that true?

You could do that, if you wish. You would just have to change the values in $templateViews (in MY_Controller) to reflect the sub-directory (for example: 'header' => 'includes/header'). I tend to think of "includes" as a location for legacy scripts, so I would go with something like "themes" or "templates", maybe with an additional sub-directory so I can setup multiple templates in the same directory (i.e. 'themes/default/header').

Quote:Secondly, with all those files in place, when i try open that project directory at the root i get the default codeigniter page? Am I supposed to specify the dir of the codeigniter site and the controller class and the private function in order to see the page put together? as you see below:

-------------------------------------------------------
/project1/MY_Controller/private
-------------------------------------------------------

Or is there something else I am missing?

Thanks again,
Josh

You only use the methods in MY_Controller through another controller, so I included the Something controller (which would go in /application/controllers/Something.php) which extends MY_Controller. That means that any public or protected method (function) or property (variable) in MY_Controller is available in the Something controller. The properties in MY_Controller which are marked "private" are only accessible by MY_Controller and its methods. That's why we have to have methods in MY_Controller which set those properties if we want the Something controller to be able to change them.

If you don't set a route, assuming the CodeIgniter site is available from http://localhost, when you go to http://localhost/something, CodeIgniter is going to look for a controller named Something.php in /application/controllers. Since there is only one URI segment, it looks for a method in that controller named index(). So, http://localhost/something and http://localhost/something/index take you to the same page, which is loaded by the controller at /application/controllers/Something.php via the index() method, which, given the example code will attempt to load the view from /application/views/Something/index.php.

If you visit http://localhost/something/xampp, it will load the same controller (/application/controllers/Something.php), but look for the xampp() method. This method will load the view in /application/views/Something/xampp.php.

If you wanted to change what URL takes you to a given controller/method, you would have to modify the routes in /application/config/routes.php.
Reply


Messages In This Thread
RE: Detailed Explanation of a Controller & Model? - by mwhitney - 09-01-2015, 09:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB