Commands 本文未发布 发布文章

未匹配的标注

Definition

Commands:

  • is a laravel artisan command. Laravel has it’s own default commands and you create your own as well.

  • provides a way to interact with the Laravel app.

  • a Command can be scheduled by a Task scheduler, like Cron Job or by the Laravel built in wrapper of the Cron Job “laravel scheduler”.

  • Commands could be Closure based or Classes.

  • “dispatch” is the term that is usually used to call a Command.

    Principles

  • Containers MAY or MAY NOT have one or more Commands.

  • Every Command SHOULD call an Action to perform its job. And should not container any business logic.

  • Ship may contain Application general Commands.

Rules

  • All Commands MUST extend from App\Ship\Parents\Commands\ConsoleCommand.

Folder Structure

 - app
    - Containers
        - {container-name}
            - UI
                - CLI
                    - Commands
                        - SayHelloCommand.php
                        - ...
    - Ship
        - Commands
            - GeneralCommand.php
            - ...

Code Samples

Example: a simple Command

<?php

namespace App\Containers\Welcome\UI\CLI\Commands;

use App\Ship\Parents\Commands\ConsoleCommand;

/**
 * Class SayWelcomeCommand
 *
 * @author  Mahmoud Zalt  <mahmoud@zalt.me>
 */
class SayWelcomeCommand extends ConsoleCommand
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'apiato:welcome';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Just saying Welcome.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $this->info('Welcome to apiato :)'); // green color
        // $this->line('Welcome to apiato :)'); // normal color
    }
}

Usage from CLI (Terminal):

php artisan apiato:welcome

Schedule Commands Execution

To Schedule the execution of a Command checkout the Tasks Scheduling page.

Define Consoles Routes

To define Console route go to app/Ship/Commands/Routes.php.

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~