benaaacademy/platform

仅是一个Laravel插件平台

安装: 7

依赖: 8

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

语言:JavaScript

类型:

1.0.10 2024-02-03 01:52 UTC

This package is auto-updated.

Last update: 2024-10-03 03:18:42 UTC


README

使用Laravel 5构建的模块化多语言CMS,介绍一个基于Laravel框架构建的具有全面功能的模块化多语言CMS。

最低系统要求

为了能够运行Benaaacademycms,您必须满足以下要求

  • PHP 8.1或更高版本
  • PDO PHP扩展
  • Mcrypt PHP扩展
  • GD PHP库
  • MySql 5.5

安装BenaaacademyCms

非常简单,您可以选择以下三种安装方法之一

首先,您必须有一个laravel 5项目,可以安装Benaaacademy/platform包

composer require Benaaacademy/platform

然后在应用配置中添加服务提供者

Benaaacademy\Platform\ServiceProvider::class

然后运行以下Artisan命令进行安装

php artisan Benaaacademy:install

享受吧 :)

现在您可以使用在安装命令期间请求的用户名和密码登录 /backend。登录后,您将能够访问 /backend URI 上的管理面板。

您还可以通过admin配置更改管理员URL前缀

<?php

return [

	/**
 	* | Admin prefix (the admin url segment)
 	* |
 	* | @var string
 	*/

	'prefix' => env("ADMIN_PREFIX", "backend"),

	/**
 	* | Default URI after user authentication
 	* | without admin prefix
 	* | @var string
 	*/

	'default_path' => env("DEFAULT_PATH", "posts"),

插件结构

插件文件夹位于根目录中,除了app文件夹外。

每个插件可能包含以下目录

  • config
  • controllers
  • lang
  • models
  • migrations
  • views
  • routes.php
  • {Name}Plugin.php

创建插件

php artisan plugin:make plugin-name --plain

您还可以使用一些额外的资源创建插件。

php artisan plugin:make plugin-name --resources

默认情况下,插件未安装。要安装,请运行

php artisan plugin:install plugin-name

###插件引导文件类

文件命名将是插件文件夹名称的大驼峰命名法,后缀为Plugin.php

示例

  • 文件夹名称为"products"的插件文件将是ProductsPlugin.php,文件夹名称为"featured_products"的插件文件将是FeaturedProductsPlugin.php。

插件类名与插件文件名相同。

####一个示例"products"插件

<?php

/*
 * Class ProductsPlugin
 */

class ProductsPlugin extends Plugin {

	/** [optional]
 	* | Plugin Service providers
 	* |
 	* | @var array
 	*/
	public $providers = [];

	/** [optional]
 	* | Plugin aliases
 	* |
 	* | @var array
 	*/
	public $aliases = [];

	/** [optional]
 	* | Admin commands
 	* | Located in plugin commands folder
 	* | @var array
 	*/
	public $commands = [];

	/** [optional]
	* | plugin middlewares are located in plugin middlewares folder
 	* | @var array
 	*/
	public $middlewares = [
		//PluginMiddleware::class
	];

   /** [optional]
 	* | Plugin routed middleware.
 	* | Located in plugin middlewares folder
 	* | These middleware may be assigned to groups or used individually.
 	* |
 	* | @var array
 	*/
	public $route_middlewares = [];

	/** [optional]
 	* | Plugin permissions
 	* | A list of plugin permissions
 	* | @var array
 	*/
	public $permissions = [
		// "create"
	];


	/** [required]
 	* Plugin details
 	* @return array
 	*/
	function info(){

    	return [
        	'name' => 'products',
        	'description' => '',
        	'version' => '0.1',
        	'icon' => 'fa-puzzle-piece',
        	'author' => '',
        	'url' => ''
    	];

	}

	/** [required]
 	* Plugin bootstrap
 	* Called in system boot
 	*/
	function boot(){

	}	

	/** [optional]
 	* Plugin install
 	* Running plugin migrations and default options
 	*/
	function install(){
    	parent::install();
	}

	/** [optional]
	* Plugin uninstall
	* Rollback plugin installation
	*/
	function uninstall(){
   		parent::uninstall();
	}

}
  • 插件信息方法返回一个包含插件详细信息的数组。
  • 插件安装和卸载方法在安装和卸载插件时调用。
  • 引导方法在系统引导时调用。
  • 在引导方法中,您可以添加一些功能,例如
    • 将任务附加到系统动作。
    • 在侧边栏菜单中创建项目。
    • 向sitemap文件中添加一些URL。
    • 创建一些计划任务。
    • 将小部件添加到仪表板。
    • 将额外的HTML输入添加到管理员表单中。
    • 添加一些插件辅助函数。

###配置目录

使用命令行创建插件后,将在config文件夹中放置一个名为模块名称的新文件,可能包含以下默认选项

您可以使用以下方法访问插件配置项的值

config("module_name.config_key");

插件控制器目录

  • 创建的控制器应扩展自BackendController类。

插件模型目录

  • 创建的模型应扩展自Model类。
这是一个示例模型
<?php

/**
 * Class Product
 */
class Product extends Model
{

/**
 * @var string
 */
protected $module = 'products';

/**
 * @var string
 */
protected $table = 'products';
/**
 * @var string
 */
protected $primaryKey = 'id';

/**
 * @var array
 * specify table fields to search using built-in scopeSearch()
 */
protected $searchable = ['title', 'content'];

/**
 * @var array
 * make slug from title and store it in slug field before creating a record
 */
protected $sluggable = ['slug' => 'title'];

/**
 * @var array
 */
protected $creatingRules = [
	'title' => 'required'
];

/**
 * @var array
 */
protected $updatingRules = [
	'title' => 'required'
];


?>
自定义插件模型验证
<?php

 /*
 * Adding some custom messages to validator
 * @return array
 */
protected function setValidationMessages()
{
    return [
    	"name.required" => "The name is required",
    	// and more
    ];
}

/**
 * Adding attributes names to validator
 * @return array
 */
protected function setValidationAttributes()
{
    return [
    	"name" => "Client name",
    	// and more
    ]
}

 /**
 * @param $v
 * @return mixed
 * run when creating or updating a model
 */
function setValidation($v)
{

    $v->sometimes("content", "required", function ($input) {
        return $input->type == "featured";
    });
    
    return $v;
}  

/**
 * @param $v
 * @return mixed
 * run when creating a model
 */
function setCreateValidation($v)
{

    $v->sometimes("content", "required", function ($input) {
        return $input->type == "featured";
    });
    
    return $v;
}  

/**
 * @param $v
 * @return mixed
 * run when updating a model
 */
function setUpdateValidation($v)
{

    $v->sometimes("content", "required", function ($input) {
        return $input->type == "featured";
    });
    
    return $v;
}  
保存模型
 <?php

 $product = new Product();

 $product->title = "A sample title";
 $product->content = "A sample content";
 
 
 // overriding model validation "if wanted"
 $product->rules(["title" => "required"], ["title.required" => "Missing title"]);
 
 if (!$product->validate()) {
    return $product->errors();	// return validation errors
 }
 
 $product->save();	
 // Return id
 // A unique slug will be created and stored without assigning it.

###迁移目录

您可以使用以下方法创建新的插件迁移文件

php artisan plugin:migration {migration_file_name} {module_name}

并运行插件迁移

php artisan plugin:migrate {module_name}

###视图目录

视图可以从任何地方通过以下方式访问

return view({module_name}::{view_file_name}); 

###Lang目录

您可以使用以下方法访问插件lang项

trans("{module_name}::{lang_file}.{lang_key}");

创建了额外的键来翻译模块名称、权限、属性和自定义验证消息

<?php

'module' => '{module_title}',

'attributes' => [
    
    'title' => 'Title',
    
    // some other fields
],

'permissions' => [
    
    'create_item' => 'Create an item',
    
    // some other messages
],

'messages' => [
    
    'required' => 'This field is required',
    
    // some other messages
]	

?>

###路由文件

<?php

Route::group([

	"prefix" => ADMIN,

	"middleware" => ["web", "auth"],

], function ($route) {

		// routes goes here
		
});

动作

  • 动作是CMS核心在执行过程中或在特定事件发生时触发的钩子。插件可以指定其PHP函数中的一个或多个在这些点执行,使用Action API。

  • 动作建立在laravel事件之上。

#####示例

<?php

// Using callbacks
Action::listen('auth.login', function($user){
	// do some tasks
});

// Or call `onLogin` method in `LoginHandler` class
Action::listen('auth.login', 'LoginHandler@onLogin');

// Or call `handle` method in `LoginHandler` class
Action::listen('auth.login', 'LoginHandler');

// Adding Action priority if there are more than one listener
Action::listen('auth.login', 'LoginHandler', 1);

开发者可能需要在管理表单中添加一些字段。让我们通过一个使用操作的例子来说明。

#####示例:向用户添加性别字段

我们将把以下代码添加到插件类的boot方法中,使用user.form.featured操作来添加字段的HTML视图。

<?php

Action::listen("user.form.featured", function ($user) {

	return view("users::custom")->with("user", $user);
	
});

custom视图中,我们将添加必要的性别字段。

<div class="form-group">
	<input name="gender"
       value="<?php echo @Request::old("gender", $user->gender); ?>"
       class="form-control input-lg" />
</div>

基本完成,只需使用user.saving操作保存即可。

<?php

Action::listen("user.saving", function ($user) {
    
    // Adding gender field to model attributes before saving it.
    $user->gender = Request::get("gender");
    
    // some validation rules if wanted
    $user->rules(
    	["gender" => "required"],
    	["gender.required" => "Gender field is required"], // optional
    	["gender" => "Gender"]	// optional
    );
    
});

操作API

<?php

// Authentication
Action::listen("auth.login", function($user){});
Action::listen("auth.logout", function($user){});
Action::listen("auth.forget", function($user){});
Action::listen("auth.reset", function($user){});

// Users
Action::listen("user.saving", function($user){});
Action::listen("user.saved", function($user){});
Action::listen("user.deleting", function($user){});
Action::listen("user.deleted", function($user){});
Action::listen("user.form.featured", function($user){});
Action::listen("user.form.sidebar", function($user){});

// Roles
Action::listen("role.saving", function($role){});
Action::listen("role.saving", function($role){});
Action::listen("role.deleting", function($role){});
Action::listen("role.deleted", function($role){});
Action::listen("role.form.featured", function($role){});
Action::listen("role.form.sidebar", function($role){});

// Pages
Action::listen("page.saving", function($page){});
Action::listen("page.saving", function($page){});
Action::listen("page.deleting", function($page){});
Action::listen("page.deleted", function($page){});
Action::listen("page.form.featured", function($page){});
Action::listen("page.form.sidebar", function($page){});

// Posts
Action::listen("post.saving", function($post){});
Action::listen("post.saving", function($post){});
Action::listen("post.deleting", function($post){});
Action::listen("post.deleted", function($post){});
Action::listen("post.form.featured", function($post){});
Action::listen("post.form.sidebar", function($post){});

// Tags
Action::listen("tag.saving", function($tag){});
Action::listen("tag.saving", function($tag){});
Action::listen("tag.deleting", function($tag){});
Action::listen("tag.deleted", function($tag){});
Action::listen("tag.form.featured", function($tag){});
Action::listen("tag.form.sidebar", function($tag){});

// Categories
Action::listen("category.saving", function($category){});
Action::listen("category.saving", function($category){});
Action::listen("category.deleting", function($category){});
Action::listen("category.deleted", function($category){});
Action::listen("category.form.featured", function($category){});
Action::listen("category.form.sidebar", function($category){});

// Dashboard
Action::listen("dashboard.featured", function(){});
Action::listen("dashboard.right", function(){});
Action::listen("dashboard.middle", function(){});
Action::listen("dashboard.left", function(){});

在侧边栏菜单中创建项目

<?php

Navigation::menu("sidebar", function ($menu) {
	
	$menu->item('products', trans("products::products.name"), URL::to(ADMIN . '/products'))
	
	->icon("fa-product-hunt")	// optional
	
	->order(1);				// optional
});
  • 方法item接受slug、标题和url。
  • 如上例所示,您可以通过将子项的slug设置为products.{sub_item}来创建子项。
  • slug参数必须唯一。

创建定时任务

<?php

Schedule::run(function($schedule){
 	
 	$schedule->call(function(){
    	
    	// Executing some db queries
    	
    	sleep(7);
    	
	})->cron('* * * * *')->name("task_name")->withoutOverlapping();

});

添加链接到网站地图

<?php

Sitemap::set("sitemap", function($sitemap){
	
	$sitemap->url(url("/"))
	
	->date(time())		// optional
	
	->priority("0.9")	// optional
	
	->freq("hourly")	// optional
});

此url将被添加到主网站地图sitemap.xml,位于public/sitemaps目录中。

命令

有一些命令可以帮助您快速处理插件。

plugin:list          List all plugins
plugin:make          make a plugin (--plain & --resources)
plugin:migration     Create a plugin migration file
plugin:migrate       Migrate plugin migration files
plugin:migrate:up    Make plugin migrations up
plugin:migrate:down  Make plugin migrations down
plugin:publish       Publishing plugin public and config files
plugin:install       Install a plugin
plugin:uninstall     Uninstall a plugin
plugin:update        reinstall a plugin

还有更多...

别忘了发送反馈...