Help! File Management in MVC-pattern PHP / mySQL Application
Posted on 2009-05-04
Hi experts. A grey Bank Holiday in England so a good excuse for some development :-) I'm looking for some help with my app. I think part of the problem is file management, and the rest is because I'm new to the whole world of object-oriented code. Allow me to explain...
I'm setting up a website with a remote assistance service. On my homepage, I have a login box on the right side. I want this to be a quick entry route into starting a remote assistance session.
The app needs to:
- Check if the user is logged in
- If no, display a username/password form
- If yes, display some data about that user's account, a "Welcome, [user name]" message, and the form which allows the user to initiate the remote assistance service.
I could code this fairly quickly with a simple procedural approach, but I've had a taste of OOP / MVC and I like it! However, I'm having some trouble 'bringing it all together'. Let me explain the two immediate problems, and I will then explain my app architecture:
I have separated my files as below. My core classes, business model, controllers and views are all in separate folders. Let's say my db_connect script needs to 'know about' my db core class to perform its actions. Should I include() the db core class in the db_connect script, or should they all be glued together elsewhere?
My understanding is that the controller should take user input, consult the model, and then generate a relevant view. Rather than generate two entire homepages depending on whether the user has logged in or not, how should I configure my app so that I can have one homepage 'view' with two alternative login areas?
Core Classes (ones that I could use across multiple apps)
"html.php" - class to simplify generation of a standard HTML page (meta tags, google analytics code etc..)
"form.php" - class to simplify HTML Form code generation
"db.php" - class to handle connections and queries
"user.php" - class to handle a user object with login, data collection and logout methods
Straight script. Creates a "db" object from core classes, and calls a method with (hostname, username, password) parameters to open the database connection.
Checks to see if the login form has been submitted.
If yes, the "login" method in my "user" core class is called. The database is queried to check the username and password match. If yes, session variables are created for future authentication.
A mix of HTML and object-oriented PHP built from my core classes. No logic, just output.