Welcome Guest, Not a member yet? Register   Sign In
Best practise - controllers
#1

[eluser]Unknown[/eluser]
Hey guys.

I just started out with CodeIgniter which I'm already really fond of. Even though it's easy and simple to use, I have one problem regarding structuring of the MVC-pattern.

What is the best practice when structuring controllers in CodeIgniter/MVC-pattern? How would it be considered most correct/best practise to organize them?

Would it be best to have every single module seperated in a file for it self, or should I try gathering modules inside larger and more general methods/controllers?

As an example, while I was programming a login controller that includes login, processing, logout, request password, login activation, etc. I was having some trouble with the naming with the login controller, as I would name my main login method login() inside the login controller, which was also named login. Instead I had to rename the method to logmein() which would make the url look ridiculous: www.site.com/login/logmein/

Also, is there anything in this matter that would be smarter when it comes to recycling and sharing methods across the web app?
#2

[eluser]rogierb[/eluser]
This is something that I presume keeps many of usy quite busy.

As for you login controller, why call it login as it does so much more? Why not call it user controller as it handles the user? or authorization controller as in handles authorisation?

Not quite that easy, is it?
What I mostly do is see what urls it generates and see what terminology can be used to capture all the different methods.

In the case of or login controller I would go for the name 'user'
http://www.site.com/user/login
http://www.site.com/user/logout
http://www.site.com/user/forgot_password_yet_again
#3

[eluser]jedd[/eluser]
Hi pdev and welcome to the forums.

Searching on 'best practice' in the forums will likely reveal many similar questions, with a wealth of answers.

[quote author="pdev" date="1259084758"]
As an example, while I was programming a login controller that includes login, processing, logout, request password, login activation, etc. I was having some trouble with the naming with the login controller, as I would name my main login method login() inside the login controller, which was also named login. Instead I had to rename the method to logmein() which would make the url look ridiculous: www.site.com/login/logmein/
[/quote]

My usual response to this type of confusion is to encourage the person to think of classes as nouns, and the methods within as verbs.

That is, the class refers to a resource, a thing, an entity.

The method refers to something you do with, or to, that entity.

It's not quite Sapir-Whorf level, but I do believe that badly named constructs in your application will make it much harder for someone to get their head around fundamental logic or design flaws.
#4

[eluser]Stilly[/eluser]
[quote author="rogierb" date="1259085518"]
What I mostly do is see what urls it generates and see what terminology can be used to capture all the different methods.[/quote]

Hi, I am playing around with my first CI application and I realized that I am doing the same. I am starting to create controllers and function names according to SEO rules and with the idea of "speaking urls" in mind.

I just wonder if this is the right approach when developing software? On the other hand: Thinking of nouns and verbs/attributes (as Jedd said already) brings the very idea of OOP back to live...

Sven
#5

[eluser]stuffradio[/eluser]
I've separated my Controller files and functions to something that makes sense.

So, say you want to login, "Members/login". I'm working on my admin panel section right now. This is a module using HMVC.

I agree with Stilly and "speaking urls" in mind. Some controller names I have are Contests, Ranks, Settings. If I wanted to add a Rank, it'd be "Ranks/add" or "Settings/view".

Just try and be as user friendly as you can. You can add security using MY_Controller so people can't just guess a URL and be at a place accidentally.

I hope I didn't ramble too much, but yeah that's what I do.




Theme © iAndrew 2016 - Forum software by © MyBB