1. Go to this page and download the library: Download media-store-net/wp-mvc-core library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
media-store-net / wp-mvc-core example snippets
// Path to the controllers folder.
$engine = new MediaStoreNet\MVC_Core\Engine(
// Path to the views folder.
$views_path = 'path_to_views_folder',
// Path to the controllers folder.
$controllers_path = 'path_to_controllers_folder',
// Namespace of your plugin or theme. For controller access
$namespace = 'MyApp'
);
namespace MyApp\Models;
use MediaStoreNet\MVC_Core\Model as Model;
use MediaStoreNet\MVC_Core\Traits\FindTrait as FindTrait;
class Post extends Model
{
use FindTrait;
}
$post = new MyApp\Models\Post();
$post->post_title = 'New post';
$post->save(); // New inserts.
// Find and get post model from id.
$post = MyApp\Models\Post::find( $post_id ); // Returns null if not found
// Delete post (move it to trash)
$post->delete();
// Update an attribute
$post->post_content = 'My content';
$post->save();
// Access to post meta
$meta = $post->meta;
// Cast to array
$array = $post->to_array();
// Cast to json
$array = $post->to_json();
// Cast to WP_Post
$array = $post->to_post();
// Get posts from parent.
$posts = MyApp\Models\Post::from( $parent_id );
namespace MyApp\Models;
use Amostajo\LightweightMVC\Model as Model;
use Amostajo\LightweightMVC\Traits\FindTrait as FindTrait;
class Post extends Model
{
use FindTrait;
protected $aliases = array(
'title' => 'post_title',
'content' => 'post_content',
);
}
// Find and get post model from id.
$post = MyApp\Models\Post::find( $post_id );
echo $post->title; // Will echo post_title
$post->title = 'New title';
$post->save(); // Will save post_title
namespace MyApp\Models;
use Amostajo\LightweightMVC\Model as Model;
use Amostajo\LightweightMVC\Traits\FindTrait as FindTrait;
class Post extends Model
{
use FindTrait;
protected $aliases = array(
'title' => 'post_title',
'content' => 'post_content',
'price' => 'meta_price', // Meta key "price"
);
}
// Find and get post model from id.
$post = MyApp\Models\Post::find( $post_id );
echo $post->price; // Will echo meta value for meta key "price".
$post->price = 19.99;
$post->save(); // Will save meta value for meta key "price".
namespace MyApp\Models;
use Amostajo\LightweightMVC\Model as Model;
use Amostajo\LightweightMVC\Traits\FindTrait as FindTrait;
class Post extends Model
{
use FindTrait;
protected $aliases = array(
'title' => 'post_title',
'content' => 'post_content',
'price' => 'meta_price',
'is_free' => 'func_is_free',
);
protected function is_free()
{
return $this->price <= 0;
}
}
// Find and get post model from id.
$post = MyApp\Models\Post::find( $post_id );
echo $post->is_free; // Will return true or false depending expression.
// Function aliases can not save values though.
namespace MyApp\Models;
use Amostajo\LightweightMVC\Model as Model;
use Amostajo\LightweightMVC\Traits\FindTrait as FindTrait;
class Book extends Model
{
use FindTrait;
/**
* Post type.
* @var string
*/
protected $type = 'books';
/**
* Default post status.
* @var string
*/
protected $status = 'publish';
protected $aliases = array(
'title' => 'post_title',
'description' => 'post_content',
'year' => 'meta_year',
'publisher' => 'meta_publisher',
);
}
$book = new MyApp\Models\Book();
$book->title = 'My diary';
$book->description = 'About me';
$book->year = 2015;
$book->publisher = 'www.evopiru.com';
$book->save(); // This will save a new post of type 'book' with status 'publish'. ('draft' is default)
// Find book
$book = new MyApp\Models\Book::find( $id );
namespace MyApp\Controllers;
use MyApp\Models\Book;
use MediaStoreNet\MVC_Core\View as View;
use MediaStoreNet\MVC_Core\Controller as Controller;
class BookController extends Controller
{
/**
* Searches for book based on passed ID.
* Returns view books.profile.
*/
public function display_view( $id )
{
$book_model = Book::find( $id );
// (1) Indicates which View to return
// (2) We pass $book_model to the view as 'book'
return $this->view->get( 'books.profile', array(
'book' => $book_model,
) );
}
/**
* Searches for book based on passed ID.
* Returns book as json.
*/
public function display_json( $id )
{
$book = Book::find( $id );
// Returns book as json
return $book->to_json();
}
}
// Calls:
// Controller: BookController
// Function: display_view
// Parameters passed by: $book_id
//
// call function will echo the result.
$engine->call( 'BookController@display_view', $book_id );
// Calls:
// Controller: BookController
// Function: display_json
// Parameters passed by: $book_id
//
// action function will return the result.
$json = $engine->action( 'BookController@display_json', $book_id );
// Echo a view directly
$engine->view->show( 'books.profile', array('book' => Book::find( $book_id )) );