• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Naming / referring to controllers and models

#1
Maybe I'm missing something basic but I've found conflicting information in many different places on this. Varioius tutorials and videos seem to reference models and controllers with either lower case or upper case names freely. I've done both when trying to follow these examples and haven't noticed any errors associated with how I use names. And I haven't seen any explanation for why it's necessary or what terrible things happen if you violate this often mandated practice.

Take Codeigniter's own documentation. In https://www.codeigniter.com/userguide2/g...odels.html

Under loading the model it says:
Your models will typically be loaded and called from within your controller functions. To load a model you will use the following function:
Code:
$this->load->model('Model_name');


Here, Model_name is capialized. Understandable because that is its name.  Then they say:
If your model is located in a sub-folder, include the relative path from your models folder. For example, if you have a model located at application/models/blog/queries.php you'll load it using:
Code:
$this->load->model('blog/queries');
'queries' here appears to be the file name (without the php extension) that holds the model class object named 'Queries'. Not the Model_name.


So, are they saying if it's in a subdirectory then you reference the model with its filename minus the extension? But if it's not in a subdirectory then  you reference it by it's object name?

That seems unlikely. But like I said I might be  missing something very basic. Any help appreciated.
Reply

#2
If you mix readings from the version 2 user guide (where class filenames were lower-case) with readings from the current version 3 userguide (where class filesnames are UCfirst), you will find conflicting information!

Use the user guide for the version of CodeIgniter you have installed, and there should be no confusion.

There could still be confusion searching online, as outdated webpages may still refer to version 2, which is now alegacy version and no longer supported.
Reply

#3
This mattered little (if at all) in CI 2, and you are linking to the CI 2 documentation.
It also doesn't matter on Windows, which I assume you're testing examples on, because its underlying file-system is not case-sensitive.

When you actually deploy your application, it will likely run on a UNIX-based OS, and it does matter there. Especially with CI 3, which requires Ucfirst naming for anything that is a class.
Reply

#4
Thanks much for the replies. This could explain why I've  been getting so many http Internal Server Errors that I could only fix after patiently trying to rewrite each line of object code in various ways. And then if it finally worked, having no idea why. Yes, I've been trying to learn CI using V3.1.2. The Help Docs at the top of the page clearly say "CodeIgniter User Guide Version 2.2.6".

If anyone reading this is working on CI documentation, the current Help Docs for V3xx don't have any version number at the top of the Help Doc pages including the first page of "General Topcs". I think it would be very useful to have that information at the top of each help topic first page (as with V2). Perhaps even with a caveat to the effect that CI syntax and other code-writing rules have important differences between CI  versions and one should be sure to use the correct docs.
Reply

#5
But there's more:

Reading the current version 3.1.2 of the Help Docs for the 'Models' topic I find this:

************** Example Start ****************************************

The file name must match the class name. For example, if this is your class:
class User_model extends CI_Model {
........
}

Your file will be this:

application/models/User_model.php

****************** Example Stop *******************************

That makes perfect sense. But right after that I find:

****************** Example Start **************************

Loading a Model
Your models will typically be loaded and called from within your controller methods. To load a model you will use the following method:
$this->load->model('model_name');

******************* Example Stop *****************************

Here I''m wondering how a model name could possibly start with a lower case letter. Is the rule actually that when one references a model from within a controller that the file name (argument) must not only have it's extension removed but the first char of that name must be set to its lower case? Or is it actually the model name not the file name that should be the argument?
Reply

#6
"Help Docs"?

The official CI 3 user-guide does have a version on every page.
Reply

#7
Quote:Here I''m wondering how a model name could possibly start with a lower case letter. Is the rule actually that when one references a model from within a controller that the file name (argument) must not only have it's extension removed but the first char of that name must be set to its lower case? Or is it actually the model name not the file name that should be the argument?

First, the file and class names must match so think of the "argument" any way you like.

The actual file's name MUST have an uppercase first letter.

Code:
/* WRONG */
some_model.php

/* RIGHT */
Some_model.php

In the class definition the class name must be uppercase first

Code:
/* WRONG */
class some_model extends CI_Model{

/* RIGHT */
class Some_model extends CI_Model{

However, when loading a model you can (but don't have to) use the initial lowercase letter. The loader will convert it to uppercase when time comes to find and "require" the actual file.

Whatever you use to load it, you must use the exact same string to work with it. In other words if you do this

Code:
$this->load->model('Some_model');

The you must utilize the object like this

Code:
$this->Some_model->some_function($arg);

If you don't you will get errors.

The file extension should not be added when loading models. That gets handled for you by the framework. Think of it as a convenience that saves you typing.
Reply

#8
"First, the file and class names must match so think of the "argument" any way you like."

Well, that seems to be a clear explanation and I now think I've got it. Unfortunately, the underlying syntax seems ambiguous.

The Help Docs would be more useful if they explained it as well as you did.

And also if they warned of the ambiguity and told how to avoid it.

Better yet they could just describe the syntax as:

All class names and the names of the files that hold their definitions  must match and have upper case first characters. These capitalized class or file names should be used in all CI statements that reference them.

And just to help newbies better grasp what's happening they should state that the CI load() method is looking for either the class name or the file name that holds it, as the case may be.

And then of course the code examples shouldn't show load statements like these:

$this->load->model('model_name');

$this->load->model('blog/queries'); etc.

None of the examples under Loading a Model show a single capitalized argument for the load method.

**************************************************************

Thanks for taking the time to answer.
Reply

#9
There is much that is optional in codeigniter. However i would STRONGLY encourage you to not load or call models upper case first. Yes you can do it. And yes there are probably a few people who do it. However its absolutely the convention to use lower case first. All of the examples you are going to look at, all of the common tutorials, almost every example on this forum, on stackoverflow - its all going to be lower case first. So just use that convention.
Reply

#10
(11-07-2016, 03:33 PM)ciadmin Wrote: If you mix readings from the version 2 user guide (where class filenames were lower-case) with readings from the current version 3 userguide (where class filesnames are UCfirst), you will find conflicting information!

Use the user guide for the version of CodeIgniter you have installed, and there should be no confusion.

There could still be confusion searching online, as outdated webpages may still refer to version 2, which is now alegacy version and no longer supported.

Yeah, For the last 4 months I've been reading every online article and YouTube tutorial I could find on CI. I've also paid for a few online courses thru Envato, Udemy, Lynda etc. I don't remember seeing anything about CI versions having different syntax requirements. I could have missed it though. I suppose the authors would want to create the impression that their video or web site advice could be universally applied to any CI project using any version - which is the impression I came away with.   Confused
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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