tags:

views:

279

answers:

6

how is mvc architecture used in php without any framework?

+11  A: 

I think generally using one of the common frameworks is probably the way to go. The reason is that many good developers have spent a long time writing, bug-fixing, tweaking and polishing to create something solid for basing your site on. The best thing to do is to find one you like, learn it and stick with it (unless you find a reason not to). When I work with PHP, my choice is generally Zend Framework, but there are also CodeIgniter, Symfony, CakePHP and a bunch of others.

If you still want to use the MVC pattern without an existing framework, you either have the choice of putting your own together or just logically separating each concern out from each other - this is the core tenet of MVC, the frameworks just help you achieve it.

Rasmus Lerdorf wrote about his minimal approach to the MVC pattern in PHP in 2006. Might be worth a read. You may also be interested in a mini-framework such as F3::PHP (PHP 5.3+ only) - looks pretty promising.

Splash
+1 don't reinvent the wheel
Paolo
+1  A: 

Try integrating Pear DB Layer, Smarty, PHP GACL in your core code to achieve an MVC architecture.

Aneeska
+2  A: 

It's not. Core PHP is a "start in global namespace statement and expression oriented language". You need extra code (and an optional URL Rewriter) to implement any kind of MVC architecture. That extra code is your framework.

Alan Storm
I disagree. MVC is an abstract concept. You don't need to use object oriented programming style to realise it.
troelskn
A: 

To achieve a MVC pattern you just have to separate your data persistence code ("model", mostly database stuff), the main application logic ("controller") and your presentation to the outside world ("view", like HTML pages or RSS feeds).

IF you just don't mix these three parts in your code, you already have a really basic MVC architecture. Just build distinct classes for your model, view, and controller layers, come up with a well structured way how they talk to each other and then stick to it!

For the sake of code maintainability you should ALWAYS try to work that way.

Techpriester
The model is **not** just or mostly the database! The model is your actual application. It contains everything that doesn't fall into the responsibility of controller and view. This can be domain objects, service layers, persistence layers, *everything*.
Gordon
Yes, that's right. You can fill entire books, even bookstores about the model layer. But for keeping things simple, I think it's sufficient to say "It's mostly your database abstraction" since 90% of all model implementations actually handle databases.
Techpriester
+2  A: 

You can check the PHP MVC Tutorial to find out how to use simple MVC pattern from scratch, not in an existing framework.

php html