amostajo/lightweight-mvc

一个轻量级(小型)的MVC框架,用于Wordpress插件和主题。

v1.0.5 2015-09-24 16:08 UTC

This package is auto-updated.

Last update: 2024-09-21 20:10:37 UTC


README

Latest Stable Version Total Downloads License

轻量级MVC是一个小巧的框架,它为您的自定义Wordpress插件或主题添加了模型视图控制器

轻量级MVC利用现有的Wordpress功能,防止过载已经负担沉重的Wordpress核心。

此框架受到Laravel的启发,旨在以高效、优雅和优化的方式将MVC设计模式添加到Wordpress开发中。

需求

  • PHP >= 5.4.0

安装

添加

"amostajo/lightweight-mvc": "1.0.*"

到您的composer.json文件中。然后运行composer installcomposer update

注意:如果您不使用composer,您可以下载ZIP文件,但需要手动包含文件,因为没有自动加载将被生成。

配置

您的项目需要将模型视图控制器存储在某个地方,并且轻量级MVC必须知道它们的位置。

创建这些文件夹,如下所示

[PROJECT ROOT]
 |---> [controllers]
 |---> [models]
 |---> [views]

然后在您的functions.phpplugins.php文件中,将这些设置在非常开始的地方,如下所示

// Path to the controllers folder.
$engine = new Amostajo\LightweightMVC\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'
);

使用

模型

轻量级MVC中,末尾的模型只是WordPress文章的一种类型。将您的模型存储在models文件夹中。

以下是一个模型示例

<?php

namespace MyApp\Models;

use Amostajo\LightweightMVC\Model as Model;
use Amostajo\LightweightMVC\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 );

别名

模型将允许您为文章属性、元值和事件添加别名。

<?php

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

您可以使用meta_前缀无缝地将元表扩展到文章属性,当添加别名时,模型会这样做

<?php

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".

有时您可能需要在属性中添加一些逻辑,您可以通过添加一个使用func_前缀的函数别名来完成此操作

<?php

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.

别名字段在将模型转换为数组、JSON或字符串时将被包含。

类型

如前所述,模型是文章类型。一旦您通过插件或主题注册了文章类型,您就可以为它创建一个模型。

以下示例中创建了一个新的文章类型books,用于处理书籍记录

<?php

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 );

视图

这是轻量级MVC的模板微型引擎。

视图通常是HTML内容的PHP文件。应该仅使用PHP标签来输出变量传递的值。

以下是一个视图示例,它显示上面示例中的书籍对象

<div class="book book-<?php echo $book->ID ?>">

	<h1><?php echo $book->title ?></h1>

	<ul>
		<li>Year: <?php echo $book->year ?></li>
		<li>Publisher: <?php echo $book->publisher ?></li>
	</ul>

	<p><?php echo $book->description ?></p>

</div>

注意:视图中没有PHP逻辑,只有纯HTML,这对于设计师处理来说非常完美。逻辑放在控制器中。

您可以在views文件夹中放置您的视图。例如,假设上面的视图文件名为profile.php,您可以将其放置在以下路径中

[ROOT]
 |---> [views]
 |      |---> [books]
 |      |      |---> profile.php

在此引擎中定位此视图的关键定位器将是

books.profile

注意: 如果这个视图位于插件中,您可以在任何主题中添加相同的层次结构来使用主题的样式自定义视图。轻量级MVC 将优先考虑位于主题中的视图。

类似这样

[THEME_ROOT]
 |---> [views]
 |      |---> [books]
 |      |      |---> profile.php

控制器

控制器用于创建处理任何业务逻辑和算法。

在以下示例中,我们将使用控制器来显示图书资料。

<?php

namespace MyApp\Controllers;

use MyApp\Models\Book;
use Amostajo\LightweightMVC\View as View;
use Amostajo\LightweightMVC\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 )) );

助手

本软件包附带一个请求类辅助工具,用于从GET、POST或WORDPRESS的query_vars中检索值

$name = Amostajo\LightweightMVC\Request::input( 'name', $default_value );

第三个值将清除源值,非常适合防止与NONCE相关的循环漏洞

$nonce = Amostajo\LightweightMVC\Request::input( 'my_nonce', $default_value, $clear_source = true );

编码指南

编码混合了PSR-2和WordPress PHP指南。

许可

轻量级MVC 是在MIT许可证下分发的免费软件。