PHPackages                             pwpf/demo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. pwpf/demo

ActiveProject

pwpf/demo
=========

v1.0.1(5y ago)2182[1 issues](https://github.com/pwpf/demo/issues)MITPHP

Since Mar 6Pushed 3y ago1 watchersCompare

[ Source](https://github.com/pwpf/demo)[ Packagist](https://packagist.org/packages/pwpf/demo)[ RSS](/packages/pwpf-demo/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (4)Dependencies (1)Versions (9)Used By (0)

### Installation

[](#installation)

Release

```
composer create-project pwpf/demo project_name
cd project_name
./pwpf-generator.sh
composer dump-autoload
git config --local core.hooksPath .githooks/
composer update

```

Or dev

```
composer create-project pwpf/demo --stability=dev project_name_dev
cd project_name_dev
./pwpf-generator.sh
composer dump-autoload
git config --local core.hooksPath .githooks/
composer update

```

MVC Plugin Boilerplate for WordPress
====================================

[](#mvc-plugin-boilerplate-for-wordpress)

WordPress being Event driven system, it is difficult to follow MVC Design Pattern while creating a WordPress Plugin. This project aims to help plugin developers achieve MVC pattern in their coding. If you are new to the term MVC and have never worked with MVC architecture before, I would highly recommend going through this course:

Why?
----

[](#why)

The original [WordPress Plugin Boilerplate](https://github.com/DevinVinson/WordPress-Plugin-Boilerplate) is great starting point for creating small plugins. So if your plugin is small, I definitely recommend using that boilerplate. However, as the plugin starts growing &amp; we add more-n-more features to it, it somewhat becomes challenging to decide where a certain piece of code should go OR how/when to separate different functionalities. When these things are not clear in long term project to the developer, they end up creating GOD classes that try to do everything.

The objective of this boilerplate is to separate concerns. Developer gets a chance to write individual `Model`, `View` &amp; `Controller`. Also, the concern of whether to load a controller/model or not is delegated to `Router`, so that your controller &amp; model can focus only on what they are supposed to do.

Because this project is meant to be a boilerplate, it has only those features which are required to build plugin in MVC way - No ORM - No Extra Goodies - No Huge Learning Curve.

Architecture
------------

[](#architecture)

Here is a bird eye's view at the architecture

[![MVC Architecture](https://raw.githubusercontent.com/pwpf/demo/master/docs/assets/mvc-architecture.png)](https://raw.githubusercontent.com/pwpf/demo/master/docs/assets/mvc-architecture.png)

Installation
------------

[](#installation-1)

The Boilerplate can be installed directly into your plugins folder "as-is". You will want to rename it and the classes inside of it to fit your needs. For example, if your plugin is named 'example-me' then:

- rename files from `plugin-name` to `example-me`
- change `plugin_name` to `example_me`
- change `plugin-name` to `example-me`
- change `Plugin_Name` to `Example_Me`
- change `PLUGIN_NAME_` to `EXAMPLE_ME_`

It's safe to activate the plugin at this point. Because the Boilerplate has no real functionality there will be no menu items, meta boxes, or custom post types added until you write the code.

Getting Started
---------------

[](#getting-started)

We'll try to create a shortcode that prints 10 posts that will help you understand how this boilerplate works. The guide assumes that you have gone through Installation steps and created `Example Me` Plugin.

### 1. Writing your first Router 📡

[](#1-writing-your-first-router-)

Routes can be defined inside `routes.php` file. Here is how a route can be defined for our example

```
// Full Class Name with Namespace
$router
    ->registerRouteOfType( RouteType::FRONTEND )
    ->withController( 'Example_Me\App\Controllers\Frontend\Print_Posts_Shortcode@registerShortcode' )
    ->withModel( 'Example_Me\App\Models\Frontend\Print_Posts_Shortcode' );

// ------------- OR --------------------

// Class Names Without specifying Namespaces explicitly. Boilerplate will automatically figure out the class based on the Route Type.
$router
    ->registerRouteOfType( RouteType::FRONTEND )
    ->withController( 'Print_Posts_Shortcode@registerShortcode' )
    ->withModel( 'Print_Posts_Shortcode' );
```

> It is highly recommended to go through [`routes.php`](https://github.com/sumitpore/wordpress-mvc-plugin-boilerplate/blob/master/plugin-name/routes.php). You will get to know list of all available route types &amp; examples in that file.

### 2. Writing your first Controller 🎮

[](#2-writing-your-first-controller-)

The boilerplate converts Class Name to a file name &amp; loads that file automatically.

We have passed `Example_Me\App\Controllers\Frontend\Print_Posts_Shortcode` as a controller in our `routes.php`. Boilerplate resolves this class name to file `example-me/app/controllers/frontend/class-print-posts-shortcode.php`

Any controller that is a part of a Routing (Read: added in `routes.php`) **MUST** extend `Base_Controller` class.

- If it is Dashboard (admin) related controller, then it should extend `Plugin_Name\App\Controllers\Admin\Base_Controller`.
- If it is Frontend related controller, then it should extend `Plugin_Name\App\Controllers\Frontend\Base_Controller`.

Every controller that extends `Base_Controller` **MUST** have `register_hook_callbacks`method. This method is defined as `abstract` in `Base_Controller`.

`register_hook_callbacks` method register callbacks for actions and filters. Most of your add\_action/add\_filter will go into this method.

`register_hook_callbacks` method is not called automatically. You as a developer have to call this method where you see fit. For Example, You may want to call this method in the route itself with `@` , if you feel hooks/filters callbacks should be registered when the new instance of the class is created. This way, we don't have to pollute constructor with add\_action &amp; add\_filter.

The purpose of this method is to set the convention that first place to find add\_action/add\_filter is register\_hook\_callbacks method.

> NOTE: If you create a constructor inside a controller extending `Base_Controller`, then make sure you call `init` method inside that constructor. That means your custom constructors need to have this line `$this->init( $model, $view );` to set `Model` &amp; `View` for your controller object.

 **SHOW CONTROLLER EXAMPLE CODE** Here is how this file would look for our example ```
