Welcome Guest, Not a member yet? Register   Sign In
Controller function naming - Best Practices?
#1

[eluser]dabadguy[/eluser]
Hi everyone,

Been using other MVC frameworks for a while but am loving CodeIgniter. One thing I have never been quite sure about with MVC is knowing when to separate the functions into different controllers.

Problem:

Site has admin control panel.

Admin logs in through www.site.com/admin/index

Admin wants to add a user through www.site.com/admin/addUser and takes them to a form where they enter the new user details

When form is submitted the action goes to www.site.com/admin/userAdded in this function, the information submitted into the form is added to the data base through the user model. After user is added, browser goes back to www.site.com/admin/index

My question is when the form is submitted should it go to www.site.com/user/addUser ?

The way I have it set up now I find it a bit confusing to have admin/addUser and admin/userAdded

Just curious to see what others do or what the best practice is.

Many thanks,
#2

[eluser]bretticus[/eluser]
I submit forms to themselves. This works well with the form_validation run method because the input fields can get error messages or keep state between posts. I usually call a redirect on success (after processing.) That way the form cannot be resubmitted accidentally Wink

Also, not a fan of using caps in my URLs. Neither is CodeIgniter (check out the style guide.)
#3

[eluser]dabadguy[/eluser]
[quote author="bretticus" date="1265968496"]I submit forms to themselves. This works well with the form_validation run method because the input fields can get error messages or keep state between posts. I usually call a redirect on success (after processing.) That way the form cannot be resubmitted accidentally Wink[/quote]


Ah, OK. I think I catch your drift. Sounds good, thank you for clearing that up.

If you have some examples I'd love to seem them. I've been looking around for a fully functional sample application where I can look at the code. I learn best by exploring that way.
Quote:Also, not a fan of using caps in my URLs. Neither is CodeIgniter (check out the style guide.)

Is camel case bad for any particular reason?

Thanks again
#4

[eluser]jedd[/eluser]
You weren't explicit, but I'm guessing that Admin is your controller here?

How about within Admin, you have a user method, and the first parameter defines (and then sends you off to wherever) what you are doing with the user (eg. add, edit, delete). This way you can do your auth/perm checks in the user method, before jumping off to (probably _ prefixed) private functions.

Bretticus is right about sending back to 'self' with forms - it seems to work pretty well for most people, but of course there will be exceptions (so don't try to force it if you're finding it more work than it's worth).

CamelCase - a holy war topic, to be sure, but nothing in CI uses it - so your apps will have a mix of styles if you stick with it - plus people around here tend to be aware of, but eschew it. This might be a small thing for you, but if you post code you'll find some people just find it frustrating and put less effort into working through samples.

Happily (for me) I've never really liked CamelCase - it's an ugly solution to a problem that has much better solutions, I've always thought. But I understand that for people that are used to it, it's a bit of a shift. No obligation, of course. Just be aware of the model, library and controller naming conventions (defined in the FAQ and the user guide) - beyond that you can pretty much do what you like with function names.
#5

[eluser]dabadguy[/eluser]
Hey Jedd I like your idea about having the user function and giving it parameters. I'll give it a shot.

Thanks for the info on camelCase. It's our standard at work so it's a hard habit to break but will try.
#6

[eluser]Neeraj Kumar[/eluser]
I personally use '-' or '_'
#7

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...uting.html
#8

[eluser]jedd[/eluser]
[quote author="dabadguy" date="1266004184"]Hey Jedd I like your idea about having the user function and giving it parameters. I'll give it a shot.
[/quote]

As a general rule, you should try to follow the controller = noun (or resource or entity or thing) and your methods are verbs that do stuff to that resource/entity/thing. So this breaks that approach, however general rules are meant to have exceptions.

If you're happy to ponder the situation a bit deeper, consider ways of not having an Admin controller at all - spreading that functionality through the site proper. It actually turns out to be not that hard to design and do up front (hard to retro-fit, though, I grant you).

You can - as has just been pointed out - work around all this stuff with routing. But I recommend you avoid routing until you've really painted yourself into a corner that offers no other way out.

Quote:Thanks for the info on camelCase. It's our standard at work so it's a hard habit to break but will try.

Just so. Having never been a professional programmer, I've managed to avoid this conundrum.

Perhaps present a case to change to your workplace - a few OOImpress slides explaining why they're wrong. Wink
#9

[eluser]dabadguy[/eluser]
Quote:If you're happy to ponder the situation a bit deeper, consider ways of not having an Admin controller at all - spreading that functionality through the site proper. It actually turns out to be not that hard to design and do up front (hard to retro-fit, though, I grant you).

OK I'm just trying to get an idea of what the site design would be then. Say I had a simple site with 1 database table called users. Has 4 fields, ID, username, password, accesslevel.

User goes to the site and is presented with a login form.

User logs in, if they are admin level, can see all users and add new users. If they are not admin level, they just see their user info (username and accesslevel).

What would I have for controllers?

Thanks!
#10

[eluser]SitesByJoe[/eluser]
As others have said, I post my forms to themselves since the form_validation library is excellent and I always use it.

Here's some of my admin controller/function naming examples

Controller:

admin

Functions:

admin/login - posts to itself and redirects the admin/dashboard if login is successful.
admin/logout

Some of my admin area controllers have an admin_ prefixed to their names if there's matching controller names in the public side of things.

admin_users - shows all the users
admin_users/add - adding a user and the form posts to the same function
admin_users/edit - same idea

OR

admin_galleries
admin_galleries/add
admin_galleries/edit
admin_galleries/photos

ETC

Looking back I should just nest them all in an admin folder to remove the need of the naming prefix. A bad habit I picked long ago before MVC...

Definitely read the form_validation user guide a bunch of times so you absorb the process. It's really nice. If you put your validation rules into the config file - it really kicks ass!

I just discovered using the config file not too long ago and it's changed the way I do everything with forms.




Theme © iAndrew 2016 - Forum software by © MyBB