Membuat Console Command Pada Perintah Artisan di Laravel

 


pada tutorial kali ini kita kana belajar bagaimana menambah perintha artisan dilaravel sesuai dengan apa yang kita butuhkan jadi langsung saja.

pertama silahkan buat project baru dengan perintah

composer create-project laravel/laravel nama_project

tunggu selesai create project, pada contoh kali ini saya akan memcoba membuat class helper

selanjuttnya kita akan menambah perintah artisan dengan perintah 

php artisan make:command MakeHelper

selanjutnya buka file MakeHelper yang berhasil di buat ada pada folder 

app/Console/Command/MakeHelper.php

<?php

namespace App\Console\Commands;

use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class MakeHelper extends Command
{

    protected $basePathHelper = 'App\Helpers';

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:helper {helper : The name of the helper}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new helper';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $helperName = $this->argument('helper');

        if ($helperName === '' || is_null($helperName) || empty($helperName)) {
            $this->error('Name Invalid..!');
        }

        // create if folder Contracts not exists 
        if (!File::exists($this->getBaseDirectory($this->basePathHelper, $helperName))) {
            File::makeDirectory($this->getBaseDirectory($this->basePathHelper, $helperName), 0777, true);
        }

        $title = Str::remove(' ', ucwords(Str::of($helperName)->replace('_', ' ')));

        $eloquentFileName = 'app/Helpers/' . $title . '.php';

        if (!File::exists($eloquentFileName)) {
            $eloquentFileContent = "<?php\n\nnamespace " . $this->basePathHelper

                . ";\n\nclass " . $helperName
                . "\n{\n\n}\n";

            File::put($eloquentFileName, $eloquentFileContent);

            $this->info('Helper Created Successfully.');
        } else {
            $this->error('Helper Files Already Exists.');
        }
    }


    protected function getBaseDirectory($basePath, $helperName)
    {
        return File::dirname($basePath . '\\' . $helperName);
    }
}

selanjutnya ubah file file Kernel.php yang ada pada folder

app/Console/Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\MakeHelper;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        MakeHelper::class
    ];
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
Jika sudah silahkan cek perintah artisan yang telah di tambahkan dengan mengetikkan perintah

php artisan list

selanjutnya silahkan jalan perintah artisan tersebut until Membuat class helper baru dengan perintah

php artisan make:helper TestHelper

jika berhasil maka file yang berhasil di digenerate ada pada folder  app/Helpers/TesHelper.php

Sekian dulu tutorial kali ini dan terima kasih.

No comments

Powered by Blogger.