🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# :-: 发布配置以及 migrations ### 命令发布 > 在command目录下面建立 Publish.php 文件 > 在config建立 permissions.php 配置文件 > 在database目录下建立相关表的 migration 文件 > 这里要提到一下,你可以首先下载 think-migration 包,然后建立所需要的表结构 这里为什么可以这么做? ``` 1. 得益于5.1对于 config 的统一层次管理,这很适合编写包的配置文件, 因为之间没有关联。可能你会问?5.0难道不可以吗?extra的配置也是可行的。没错!所以我讲的是对开发更加友好了。 2. migration 的加入,适合我们编写与包关联的表操作,虽然 5.0 也可以,但是我更希望的是官方能加入支持。这样会省去开发过程 ```` ### 编写命令发布 这里会直接贴上发布代码 ```php <?php namespace think\permissions\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; class PermissionPublish extends Command { protected function configure() { $this->setName('permission-publish') ->setDescription('Publish Permission Files'); } protected function execute(Input $input, Output $output) { $output->write(sprintf('permissions config file publish %s' . PHP_EOL, $this->publishConfig() ? 'successfully' : 'failed')); $output->write(sprintf('permissions migrations publish %s',$this->publishMigrations() ? 'successfully' : 'failed')); } // 发布配置文件 protected function publishConfig() { $permissionsConfigPath = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'permissions.php'; return copy($permissionsConfigPath, app()->getConfigPath() . 'permissions.php'); } // 发布migration文件 protected function publishMigrations() { $migrationsPath = app()->getRootPath() . 'database' . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR; if (!is_dir($migrationsPath)) { mkdir($migrationsPath, 0777, true); } $databasePath = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR; $handle = opendir($databasePath); $status = true; while ( ($file= readdir($handle))!= false) { if ($file!= '.' && $file!= '..') { if (!copy($databasePath . basename($file), $migrationsPath . basename($file))) { $status = false; break; } } } return $status; } } ```