PDO MVC Framework
I'm trying to build a simple mvc framework to understand. I've heard advice about rolling you own framework but I'd rather learn by
playing with some basic examples since diving into using pre existing frameworks without understanding everything seems daunting to me.
So, I have a controller, model and view. In my database file I'm able to retrieve data using $result->title as an example, but I'd like to have
that information passed to the view (which is basically just my template file).
I'm not really sure how to do this, I've tried a couple of methods and googled, but I've got conflicting answers.
I'd appreciate it if anyone could point me in the right direction and maybe give advice for best practices for mvc structure since I got a lot of conflicting
answers on that.
I've attached some of the code I'm using below.
I too built my own MVC and I also ran into the same problem.
The way I solved it was using the extract function. For example:
Also it might be worth having a main controller that all your other controllers extend. The main controller would take care of setting scripts, styles and data for the view.
If you need any inspiration I recommend taking a look at larvel. In my opinion it is one of the best frameworks around.
I'd be happy to answer any other questions your might have :)
Edit: Just had a closer look at your code, it doesn't seem like to you are getting the data from the model. It looks like to are just setting it to a property in the model but not using it in the controller, so your view would not have access to it.
note on the Database class: you should remove the try...catch. currently, if you get no connection, the next call(s) to pdo will error out.
rule of thumb for Exceptions: catch an Exception where you can handle the problem (not where the problem occurs).
for a basic exception like a failed connection, that is rather towards the top level of your script.
|All times are GMT +1. The time now is 03:23 AM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.