Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I think, the wordpress plug-in architecture is not MVC. So I want to use/do the MVC architecture in WordPress plug-in development (like cakephp, codeIgniter ). How can I use them in plguin development. Does WordPress support CakePHP, or other frameworks?

share|improve this question
add comment

6 Answers 6

You asked multiple questions at once:

Q: I think, the wordpress plug-in architecture is not MVC.

A: Yes, that's true. It's not MVC in the Design Pattern Sense. It's MVC in a more general view: Model: Mysql, View: HTML/CSS, Controller: PHP. Main Frontend Controller is index.php, Commands are HTTP requests. The router is .htaccess (if you use pretty permalinks), the 404 controller (if you use that one) or index.php if you use the standard setup.

Q: So I want to use/do the MVC architecture in WordPress plug-in development (like cakephp, codeIgniter ). How can I use them in plguin development. Does WordPress support CakePHP, or other frameworks?

A: No, Wordpress does not support CakePHP or CodeIgniter out of the box. You can write an adapter for codeIgniter or Cake so that you can re-use an existing MVC (e.g. by encapsulating the HTTP Request and removing the slashes from request variables / the superglobals). But you can overrider Wordpress as well to just run any other PHP Framework Application as well, e.g. by adding rewriterules to the .htaccess and adding php files. It just depends on what you want to do.

share|improve this answer
add comment

You could definitely use an MVC philosophy in the development of a plugin, but you would be really silly to try to incorporate CI or Cake in addition to WP.

For instance, you could build out a plugin using the following pseudo structure (FYI, there are a ton of ways to build this, depending on your interpretation of MVC. This is just the quick example that pops into mind):

class Plugin(){
    function __construct(){ // controller?
        run conditionals/determine query
        process model
        process view
    }
    function model(){ 
        get query from database 
        return variables
    }
    function view(){
        echo html
    }
}

This might be a horrible example of a plugin structure, or even of MVC, but it is simply to illustrate that MVC architecture !== MVC frameworks and that MVC can be used in plugin design. Widget classes and other functionality throughout WP are similar, although without any explicit separation of concerns.

share|improve this answer
    
I agree see my ref to lesdorf article MVC without framework –  user310291 Mar 20 '11 at 14:45
add comment

I had this same question a while back, and at the moment, unfortunately, the only way to do MVC in a WordPress theme or plugin was to role your own.

So that's what I did, and I've been using it in my plugins and themes for a few months now. You can view the source on github here: https://github.com/philipwalton/PW_Framework It's called PW_Framework, and it's loosely based on the Yii Framework, a very good MVC framework for PHP.

At it's core, PW_Framework is a quick and easy way to create options pages complete with client and server side validation. It handles the ajax validation as well as all the WordPress security issues (like nonce), so you can focus your development efforts on what your plugin actually does.

All you have to do is define your model with the attributes, attribute labels, descriptions, and options:

public function data()
{
  return array(
    'name' => array(
      'label' => 'Name',
      'default' => '',
    ),
    'email' => array(
      'label' => 'E-mail',
      'default' => '',
    ),
    'sex' => array(
      'label' => 'Sex',
      'default' => 'male',
      'options' => array( 'm' => 'Male', 'f' => 'Female'),
    ),

  );
}

Then you define the validation rules:

protected function rules() {
  return array(
    array(
      'properties' => 'name, email, sex',
      'validator'=> array('PW_Validator', 'required'),
    ),
    array(
      'properties' => 'email',
      'validator'=> array('PW_Validator', 'email')
    ),
    array(
      'properties' => 'sex',
      'validator'=> array('PW_Validator', 'match'),
      'pattern' => '/^m|f$/'
    ),
  );
}

And the view would look something like this:

<?php $form = new PW_Form( $model ); ?>
<?php $form->begin_form(); ?>
  <?php $form->begin_section('Year Options'); ?>
  <ul>
    <li><?php $form->textfield( 'name' ); ?></li>
    <li><?php $form->textfield( 'email' ); ?></li>
    <li><?php $form->radio_button_list( 'sex' ); ?></li>
  </ul>
  <?php $form->end_section(); ?>
<?php $form->end_form(); ?>

Then in the controller, you simply write all of your code functionality.

Here's a working example of it in live use: http://wordpress.org/extend/plugins/pw-archives

share|improve this answer
add comment

The plugin architecture of wordpress by itself has nothing to do with MVC. But if you wanna do MVC WITHIN your own plugin why not. You're not obliged to use a framework, MVC is a concept which could be implemented without hevay framework see http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html

share|improve this answer
add comment

WordPress is pretty much framework for itself, it takes care of stuff that is usually covered by framework with own (or bundled) code. Well, technically parts of it is BackPress framework but it's really very internal and not too relevant.

WordPress extensions (plugins or themes either) use provided WP APIs to retrieve and modify data. It doesn't matter for WP what your extensions uses, as long as it uses proper API to interact with WP itself.

So basically you can include and use any framework, library or whatever in your plugin. As long as you use WP API properly and it makes sense for performance and licensing constraints.

share|improve this answer
2  
You're not answering the question(s) asked. –  hakre Sep 21 '10 at 22:23
add comment

I assume you mean using MVC as cakePHP and codeigniter do, not 'use an MVC framework' to create a plugin or theme. The second option would require users to download the framework you used as well as the extension files.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.