laravel ---Artisan 命令行

Artisan 是 Laravel 的命令行接口的名稱,它提供了許多實(shí)用的命令來幫助你開發(fā) Laravel 應(yīng)用,它由強(qiáng)大的 Symfony Console 組件所驅(qū)動(dòng)。

自定義命令默認(rèn)存儲(chǔ)在 app/Console/Commands
目錄中,當(dāng)然,只要在 composer.json
文件中的配置了自動(dòng)加載,你可以自由選擇想要放置的地方。
若要?jiǎng)?chuàng)建新的命令,你可以使用 make:console
Artisan 命令生成命令文件:
php artisan make:console SendEmails

上面的這個(gè)命令會(huì)生成 app/Console/Commands/SendEmails.php
類,--command
參數(shù)可以用來指定調(diào)用名稱:
php artisan make:console SendEmails --command=emails:send

代碼示例:
<?php

namespace App\Console\Commands;

use App\Store\AdminStore;
use App\Tools\Common;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Validator;

/**

  • 注冊超級(jí)管理員命令

  • Class CreateAdministrator

  • @package App\Console\Commands

  • @author 李景磊
    */
    class CreateAdministrator extends Command
    {
    // 生明變量
    protected $signature = "administrator:create";

    // 聲明變量
    protected $description = "創(chuàng)建后臺(tái)超級(jí)管理員";

    // 聲明變量
    protected static $adminStore;

    // 依賴注入

    public function __construct(AdminStore $adminStore)
    {
    self::$adminStore = $adminStore;
    parent::__construct();
    }

    public function handle()
    {
    // 創(chuàng)建管理員
    self::create();
    }

    /*
    *創(chuàng)建管理員

    • */
      public function create()
      {
      // 接收數(shù)據(jù)
      $email = $this->ask('請輸入郵箱');
      $password = $this->ask('請輸入密碼');

      // 顯示接收的數(shù)據(jù)在控制臺(tái)
      $this ->info('郵箱:'.$email);
      $this ->info('密碼:'.$password);

      // 判斷是否注冊
      if ($this->confirm('確認(rèn)注冊嗎?')){
      $this->info('正在注冊...');

       // 獲取接收到的數(shù)據(jù)
       $data = [
           'email' => $email,
           'password' =>$password
       ];
      
       // 參數(shù)驗(yàn)證規(guī)則
       $validator = Validator::make($data,[
           'email' =>'required|email|min:6|max:64',
           'password' =>'required|min:6|max:128',
       ]);
      
       // 驗(yàn)證是否是合法數(shù)據(jù)
       if ($validator->fails()) {
           $errors = $validator->errors();
           $this->table(['錯(cuò)誤'],(array) $errors->toArray());
           self::create();
           return ;
       }
       // 判斷郵箱是否以注冊
       $emailRes = self::$adminStore->getOneData(['email' => $email,'status' => 1 ]);
       if (!empty($emailRes)) {
           $this->error('已注冊,請更換郵箱!');
           self::create();
           return;
       }
      
       // 拼接數(shù)據(jù)
       $data['guid']     = Common::getUuid();
       $data['password'] = Common::cryptString($data['email'], $data['password']);
       $data['sroce']    = 1;
       $data['status']   = 1;
       $data['addtime']  = $_SERVER['REQUEST_TIME'];
      
       // 寫入數(shù)據(jù)庫
       $res = self::$adminStore->addData($data);
       // 判斷是否注冊成功
       if (!empty($res)) {
           $this->info('注冊成功');
       }else{
           $this->error('注冊失敗');
           self::create();
       }
      

      }else{
      $this->info('取消注冊');
      }
      }
      }
      記得在Kernel.php中引用下面這句話;
      Commands\CreateAdministrator::class,

在store層寫方法
第一個(gè)方法(查找一條數(shù)據(jù))
public function getOneData($where , $id = '')
{
if (empty($where)) {
return false;
}
if (empty($id)) {
return DB::table(self::$table)->where($where)->first();
}
return DB::table(self::$table)->where($where)->where('guid','!=',$id)->first();

}

第2個(gè)方法(添加到數(shù)據(jù)庫)
public function addData($data)
{
if (empty($data)) {
return false;
}
return DB::table(self::$table)->insert($data);
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 工廠模式類似于現(xiàn)實(shí)生活中的工廠可以產(chǎn)生大量相似的商品,去做同樣的事情,實(shí)現(xiàn)同樣的效果;這時(shí)候需要使用工廠模式。簡單...
    舟漁行舟閱讀 7,852評論 2 17
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 都說孩童時(shí)期是人這一生最無憂無慮的時(shí)期。和孩子在一起時(shí),再老的人、再難過的人也會(huì)被“歡樂”感染。 工作需要我接觸到...
    季婷婷閱讀 865評論 3 13
  • 感覺談戀愛就像是自己給自己找罪受
    Schrodinger滴貓閱讀 125評論 0 0
  • 這世上,令人感覺幸福的事、物很多,譬如:金榜題名、偶遇良緣、洞房花燭……可最令我掛懷的是陪著你,陪著你青蔥到白發(fā)。...
    軒轅子桑閱讀 189評論 2 1