Laravel Artisan常用命令


  1. Controller 控制器

    // 創(chuàng)建一個控制器
    php artisan make:controller XXXController
    // 創(chuàng)建Rest風格資源控制器
    php artisan make:controller PhotoController --resource
    // 指定創(chuàng)建位置 在app目錄下創(chuàng)建TestController
    php artisan make:controller App\TestController
    

    ?

  2. Model

    // 指定路徑創(chuàng)建
    php artisan make:Model App\\Models\\User(linux or macOs 加上轉義符)
    

    ?

  3. Migration 數(shù)據(jù)遷移

    // 數(shù)據(jù)遷移
    php artisan migrate
    // 創(chuàng)建遷移
    php artisan make:migration create_users_table
    // 指定路徑
    php artisan make:migration --path=app\providers create_users_table
    // 一次性創(chuàng)建
    // 下述命令會做兩件事情:
    // 在 app 目錄下創(chuàng)建模型類 App\Post
    // 創(chuàng)建用于創(chuàng)建 posts 表的遷移,該遷移文件位于 database/migrations 目錄下。
    php artisan make:model --migration Post
    

    ?

  4. Seeder 數(shù)據(jù)填充

    // 創(chuàng)建要填充的數(shù)據(jù)類
    php artisan make:seeder UsersTableSeeder
    // 數(shù)據(jù)填充(全部表)
    php artisan db:seed
    // 指定要填充的表
    php artisan db:seed --class=UsersTableSeeder
    

    ?

  5. Middleware 中間件

    php artisan make:middleware XXX
    

    ?

  6. Route 路由

    // 查看所有路由
    php artisan route:list
    

    ?

  7. Request請求,主要用于表單驗證

    php artisan make:request TagCreateRequest
    

    創(chuàng)建的類存放在 app/Http/Requests 目錄下

    <?php 
    
    namespace App\Http\Requests;
    
    use App\Http\Requests\Request;
    
    class TagCreateRequest extends Request
    {
    
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */ 
        public function rules()
        {
            return [
                'tag' => 'required|unique:tags,tag',
                'title' => 'required',
                'subtitle' => 'required',
                'layout' => 'required',
            ];
        }
    }
    

    使用時只需在對應的Controller方法里引入

    // 注意這里使用的是TagCreateRequest
    public function store(TagCreateRequest $request)
    {
        $tag = new Tag();
        foreach (array_keys($this->fields) as $field) {
            $tag->$field = $request->get($field);
        }
        $tag->save();
        return redirect('/admin/tag')
            ->withSuccess("The tag '$tag->tag' was created.");
    }
    

    ?

  8. 創(chuàng)建artisan命令行(laravel5.*版本)

    // 以下命令生成文件 app/Console/Commands/TopicMakeExcerptCommand.php
    
    php artisan make:console TopicMakeExcerptCommand --command=topics:excerpt123
    
    //在 app/Console/Kernel.php 文件里面, 添加以下
    protected $commands = [
            \App\Console\Commands\TopicMakeExcerptCommand::class,
        ];
    //激活artisan命令行。12345
    //在生成的TopicMakeExcerptCommand.php 文件, 修改以下區(qū)域
    <?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    
    class TopicMakeExcerptCommand extends Command
    {
        /**
         * 1. 這里是命令行調用的名字, 如這里的: `topics:excerpt`, 
         * 命令行調用的時候就是 `php artisan topics:excerpt`
         *
         * @var string
         */
        protected $signature = 'topics:excerpt';
    
        /**
         * 2. 這里填寫命令行的描述, 當執(zhí)行 `php artisan` 時
         *   可以看得見.
         *
         * @var string
         */
        protected $description = '這里修改為命令行的描述';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * 3. 這里是放要執(zhí)行的代碼, 如在我這個例子里面,
         *   生成摘要, 并保持.
         *
         * @return mixed
         */
        public function handle()
        {
            $topics = Topic::all();
            $transfer_count = 0;
    
            foreach ($topics as $topic) {
              if (empty($topic->excerpt))
              {
                  $topic->excerpt = Topic::makeExcerpt($topic->body);
                  $topic->save();
                  $transfer_count++;
              }
            }
            $this->info("Transfer old data count: " . $transfer_count);
            $this->info("It's Done, have a good day.");
        }
    }
    
    // 命令行調用
    php artisan topics:excerpt 
    
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • 先說幾句廢話,調和氣氛。事情的起由來自客戶需求頻繁變更,偉大的師傅決定橫刀立馬的改革使用新的框架(created ...
    wsdadan閱讀 3,194評論 0 12
  • 校園失物招領平臺開發(fā) ——基于laravel框架構建最小內容管理系統(tǒng) 摘要 ? 針對目前大學校園人口密度大、人群活...
    藍蓮花xzsky閱讀 6,340評論 8 54
  • 行香子 花色清明,云遠山高。春欲歸,煙水迢迢。 慢將竹老,遲又風飄。只人長醉,思長留,景長消。 念山山去,折花花未...
    槚生閱讀 243評論 2 13
  • 好想媽媽 想得一直在哭
    卓別林來也閱讀 237評論 0 0

友情鏈接更多精彩內容