devstudio-rs / wpmvc
WPMVC
v1.0.12
2024-09-11 13:02 UTC
Requires
- php: >=7.0
Requires (Dev)
- composer/installers: ~1.0
README
应用程序
初始化应用程序。
$config = array( 'components' => array( 'request' => array( 'class' => Request::class, ), ), ); ( new App( $config ) )->init();
应用程序及其组件将被可用。
App::$app App::$app->request->post();
模型
文章模型
class Event extends \wpmvc\models\Post_Model { public $post_type = 'event'; // Example of custom attributes with default values. public $event_date = 1707422767; public $event_location = 'Bratislava'; public function registry() : array{ return array( 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, ... ); } public function registry_labels() : array { return array( 'name' => __( 'Event' ), ... ); } }
使用模型的方法。设置属性,保存或删除。
$event = Event::find_one( 24 ); $event = new Event(); $event->set_attribute( 'post_title', 'Great Event' ); $event->set_attribute( 'event_location', 'Bratislava' ); $event->set_attributes( array( 'post_title' => 'Great Event', 'event_location' => 'Bratislava', ) ); $event->load( array( 'Event' => array( 'post_title' => 'Great Event', 'event_location' => 'Bratislava', ), ) ); $event->save(); $event->delete();
使用文章模型注册自定义文章类型
使用init
WordPress 动作注册自定义文章类型。
Event::register( array( 'labels' => array( 'name' => __( 'Events' ), ... ), ) );
分类模型
class Event_Category extends \wpmvc\models\Taxonomy_Model { public $taxonomy = 'event-category'; public function registry() : array { return array( 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'show_in_rest' => true, ); } public function registry_labels() : array { return array( 'name' => __( 'Event Category' ), ... ); } public function registry_object_type() : array { return array( 'event' ); } }
通过分类搜索文章。
$events = Event::find() ->where_taxonomy( Event_Category::class, array( 'slug' => 'taxonomy_slug' ) ) ->all();
控制器
class Site_Controller extends \wpmvc\web\Controller { public function action_index() { // Action logic. } }
注册控制器。
App::$app->router->add_route( 'site', array( Site_Controller::class, 'action_index' ) );
已注册的操作将在{host}/site/
下可用。
元框
定义元框控制器。
class Event_Options_Controller extends \wpmvc\web\Meta_Box_Controller { public function on_action( $model ) { echo View::render( 'views/events/meta-box', array( 'model' => $model, ) ); } public function on_save( $model ) { if ( $model->load( App::$app->request->post() ) ) { $model->save(); } } }
定义元框模型。
class Event_Options_Meta_Box extends \wpmvc\models\Meta_Box_Model { public $id = 'event_options'; public $controller = Event_Options_Controller::class; public function init() { $this->set_title( __( 'Options' ) ); } }
将元框模型添加到文章模型。
class Event extends \wpmvc\models\Post_Model { public $post_type = 'event'; public function init() { $this->add_meta_box( Event_Options_Meta_Box::class ); } }