<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                5.1 命令行很適合編寫一些自定義命令了,例如上節權限管理里面編寫的發布文件的一些命令。你還可以編寫腳本命令來實現業務上的一些東西。平常我們經常用到的一些 CURD 文件,所以為了后期方便的,這里快速就編寫一個命令行,此后 curd 基礎文件都使用該命令創建。 ## 如何創建 首先要做一些基礎工作,肯定需要的是一些 CURD 模板文件,然后在此基礎利用命令行創建。 ![](https://box.kancloud.cn/029f92f7be8f5719dcdeeaa4dd57aaf1_306x189.png) 依據文檔需要在 application 創建 command目錄,然后在創建一個命令文件 CurdMaker.php。然后還有 **stub** 目錄, 這里存放的是模板文件,是之后創建 CURD 的基礎。 Stub 三個文件如下, 具體根據自己的喜好定義。 ### controller.stub ``` <?php namespace app\$module\controller; use think\Controller class $controller extends Controller { public function index() { return $this->fetch(); } public function create() { return $this->fetch(); } public function edit() { return $this->fetch(); } public function delete() {} } ``` ### model.stub ``` <?php namespace app\$module\model; use think\Model; class $model extends Model { public function getAll() {} public function findBy() {} public function updateBy() { } public function deleteBy() {} } ``` view.stub 目前還沒有任何內容,后期會補上,為什么會沒有呢,因為后臺管理的模板的樣式肯定具有一定性共通特征。這里可以利用 view 繼承的特性可以進行管理,所以這里此后添加。 最后在 application 目錄下的 command.php 添加命令行 ``` 'make:curd' => app\command\CurdMaker::class, ``` ### 編寫命令 命令一共需要三個參數,[controller, model, module?], module是可選的,如果沒有的話會自動獲取 app.php 配的默認模塊名稱。下面直接上代碼。在代碼里面進行一定量的解釋。可能有點枯燥,如果有不好的地方的也可以提供評論指出,我會進行修改 ``` namespace app\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; class CurdMaker extends Command { protected $appPath; // view 默認的三個模板 protected $views = ['index', 'create', 'edit']; public function __construct() { parent::__construct(); $this->appPath = env('app_path'); } protected function configure() { $this->setName('make:curd') ->addArgument('controller', Argument::OPTIONAL, "controller name") ->addArgument('model', Argument::OPTIONAL, "model name") ->addOption('module', null, Option::VALUE_REQUIRED, 'module name') ->setDescription('Create curd option controller model --module?'); } protected function execute(Input $input, Output $output) { // 首先獲取默認模塊 $moduleName = config('app.default_module'); $controllerName = trim($input->getArgument('controller')); if (!$controllerName) { $output->writeln('Controller Name Must Set');exit; } $modelName = trim($input->getArgument('model')); if (!$modelName) { $output->writeln('Model Name Must Set');exit; } if ($input->hasOption('module')) { $moduleName = $input->getOption('module'); } $this->makeController($controllerName, $moduleName); $output->writeln($controllerName . ' controller create success'); $this->makeModel($modelName, $moduleName); $output->writeln($modelName . ' model create success'); $this->makeView($controllerName, $moduleName); $output->writeln($controllerName . 'view create success'); } // 創建控制器文件 protected function makeController($controllerName, $moduleName) { $controllerStub = $this->appPath . 'command' . DIRECTORY_SEPARATOR . 'stub' .DIRECTORY_SEPARATOR. 'Controller.stub'; $controllerStub = str_replace(['$controller', '$module'], [ucfirst($controllerName), strtolower($moduleName)], file_get_contents($controllerStub)); $controllerPath = $this->appPath . $moduleName . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR; if (!is_dir($controllerPath)) { mkdir($controllerPath, 0777, true); } return file_put_contents( $controllerPath . $controllerName . '.php', $controllerStub); } // 創建模型文件 public function makeModel($modelName, $moduleName) { $modelStub = $this->appPath . 'command' . DIRECTORY_SEPARATOR . 'stub' .DIRECTORY_SEPARATOR. 'Model.stub'; $modelPath = $this->appPath . $moduleName . DIRECTORY_SEPARATOR . 'model'; if (!is_dir($modelPath)) { mkdir($modelPath, 0777, true); } $modelStub = str_replace(['$model', '$module'], [ucfirst($modelName), strtolower($moduleName)], file_get_contents($modelStub)); return file_put_contents($modelPath . DIRECTORY_SEPARATOR . $modelName . '.php', $modelStub); } // 創建模板 public function makeView($controllerName, $moduleName) { $viewStub = $this->appPath . 'command' . DIRECTORY_SEPARATOR . 'stub' .DIRECTORY_SEPARATOR. 'View.stub'; $viewPath = (config('template.view_path') ? config('template.view_path') . $moduleName . DIRECTORY_SEPARATOR : env('app_path') . $moduleName . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR) . strtolower($controllerName); if (!is_dir($viewPath)) { mkdir($viewPath, 0777, true); } foreach ($this->views as $view) { file_put_contents($viewPath . DIRECTORY_SEPARATOR . $view .'.html', file_get_contents($viewStub)); } } } ``` 這樣一個 CURD 命令行就已經創建好了, 來使用了看看。 ## 使用 ``` php think 查看一下命令是夠存在 ``` ![](https://box.kancloud.cn/c2a05c33c6c9315430b2179d6584cec1_599x192.png) 一已經有了,來嘗試創建一下。首先使用默認的模塊 index ``` php think make:curd Chat Chat ``` ![](https://box.kancloud.cn/3e90ee5a99d2c48656ad2dba7bbdf4ac_499x142.png) ![](https://box.kancloud.cn/d165c57bd1c237ca79f3bd8fcc394684_272x195.png) 沒問題,創建成功了。 一已經有了,再來創建使用新的模塊 admin, 使用模塊的話,就必須如下使用了 ``` php think make:curd Chat Chat --module admin ``` ![](https://box.kancloud.cn/632983ffe797a64cf852ed9b42808e25_573x135.png) ![](https://box.kancloud.cn/fc013d496eb4839d885e5f9e33978e3d_301x175.png) 嗯,已經沒有問題了,可以正常使用了,以上就是編寫 CURD 命令的過程。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看