Welcome Guest, Not a member yet? Register   Sign In
Documentation problem: Naming of Controllers & Models
#1

[eluser]Unknown[/eluser]
Hi folks,

I'm attempting to make my first CI-driven application by following the user guide, and I'm getting sightly confused by some of the examples given when it comes to naming controllers and models.

The controllers page (http://ellislab.com/codeigniter/user-gui...llers.html) instructs me to create a controller called 'Blog', which I did, and seemed fine until I got to the models topic (http://ellislab.com/codeigniter/user-gui...odels.html), where things start to become a little fuzzy:

It suggests that my new model file should be...
Quote:
Code:
class Model_name extends Model {...
Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.

...which as I read it means that I should create a model called 'Blog'. Fine.. but I already created a class called 'Blog' for my controller. if you then keep reading you come across the following code:

Quote:
Code:
class Blog_controller extends Controller {

    function blog()
    {
        $this->load->model('Blog');

        $data['query'] = $this->Blog->get_last_ten_entries();

        $this->load->view('blog', $data);
    }
}

...which now suggests I am now to call my controller 'Blog_controller; and my model 'Blog' (will this affect the URL routing that I covered on the previous topic? Will my links now have to be /blog_controller/view/1 instead of /blog/view/1?). If you re-read the topic you realise the code at the top uses
Code:
class Blogmodel extends Model {

.. which suddenly attaches a 'model' suffix to the model, which hasn't been mentioned anywhere else and seems in contradiction to the previous example on the same page.

So are controllers 'Blog_controller' and models 'Blog' or are controllers 'Blog' and models 'Blogmodel' (or 'Blog_model'?). I'm sure its just a case of picking one and moving forwards at this point, but I'd quite like to know which way is 'correct', and the documentation seems really muddled as to exactly what I should be naming my classes. Hope you can see where the confusion is arising from. Smile
#2

[eluser]Pascal Kriete[/eluser]
The way it's usually done is that the model name comes from the table it describes. So if you have a 'Blog' controller, and a table called 'Entries', you would have a model called 'Entry', 'Entries' or 'Entry_model' (up to you).
#3

[eluser]Unknown[/eluser]
Thanks inparo!

The documentation really doesn't make this clear (and confuses the matter further by adding in 'Blog_controller). CI seems great, and flexibility in terms of naming models is useful (although I still think I'd prefer to be able to use a singular noun as the title of my model for correctness of naming without running into class name collisions with a controller!), but someone might want to review the documentation and make sure that the same naming scheme is used throughout, at least for the beginner sections.
#4

[eluser]zwippie[/eluser]
Also remember that the name of the controller represents the URL of the page. http://mysite.com/blog is more catchy then http://mysite.com/blog_controller.
#5

[eluser]orokusaki[/eluser]
The Answer:

Didn't read every bit of this post, but to answer the first question.

I too, was confused about this issue based on the docs - The answer is:

If you have a controller named blog, and you want create a model for it to abstract some of the db logic out of it, you should name the controller blog_model.

You cannot name the model blog also. CI will try to instantiate objects from two versions of the same class which is impossible. A fatal error will occur.

That's the reason for using the extra _model at the end, so you can maintain a similar naming convention without having any problems.

Similarly, if you intend of having a DB model class for overall db abstraction or to provide ORM-like functions to multiple controllers, you may want to name it something like Entries, or other based on the table name or whatever remains within your naming conventions.
#6

[eluser]Mat-Moo[/eluser]
I also had hassles with this last night, and notice that the loader class changes the case of the requested item to load to lower class. On unix systems this is more of a pain as I had Membermodel.php which I could then never load as it would never find membermodel.php.
#7

[eluser]orokusaki[/eluser]
I'm sorry. I should have mentioned in my post. When you name a controller or model or any class in code igniter, you'll name the class:

Super_blog or Blog, you know just the first letter of the first word caps, and an underscore between each word.

Then, you'll name the file identically, except all lowers - lower.php, or lower_lower.php




Theme © iAndrew 2016 - Forum software by © MyBB