1

im designing a way the form posts its data.

e.g if we have a login.php, if the user submit we normally post it back to login.php and process it. which means if we have other pages like register.php, editprofile.php, we have to redo the process again. so normally we would do something like this in each page:

if($_POST["btnsubmit"]) {
//do smth
}

Im thinking of doing a common postForm.php which accepts all post requests, pass the data to the respective library and process it.

is this a good idea??

1 Answer 1

1

It is definitely a good idea! What you're describing is called a controller, from the Model View Controller pattern. I recommend checking out Symfony, which is a great MVC web framework for PHP.

A single Symfony controller (with a name like actions.class.php) can handle all of the posts and gets, plus the routing to get you there. By Symfony convention, a call to http://mywebsite.mydomain.com/home will run the executeHome function in the main controller. A form on that page could, for instance, post to /attemptLogin, and (again, by convention) Symfony would run the executeAttemptLogin function in this same controller file.

Sign up to request clarification or add additional context in comments.

5 Comments

hi dorkitude, tks for the good overview of symfony. can i verify for your scenario that attemptLogin is in home controller and it will run executeAttemptLogin. let say u already have a login form in mywebsite.mydomain.com/home which u mentioned. if you have another similar login form in another page say, mywebsite.mydomain.com/login, then you would have to have a duplicate code of attemptLogin in login controller which calls executeAttemptLogin???
no, both forms can point to the URL /attemptLogin: <form method="post" action="/attemptLogin"> ... </form>
(the home, login, and attemptLogin actions would all be in the same controller)
hi. what i dun quite understand is why would login functions be placed in home controller?? wat im actually planning to do is place all login functions and operations in login module (a separate folder for modules). this module will have the function executeAttemptLogin. so when a login post is executed, postForm receives the post data, smart enough to know which modules it is, pass the data to login module main lib which calls the main(), and then call executeAttemptLogin, process it and return the results to main(). afterwhich main() would revert back to which ever page form that calls it
I haven't referred to "home controller", but rather a function in the One True Controller. If it makes sense for you to architect your code in the way you describe, then go for it! In my opinion you can't go wrong unless you violate the only principle I 100% subscribe to: Don't Repeat Yourself - c2.com/cgi/wiki?DontRepeatYourself

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.