Skip to content

Instantly share code, notes, and snippets.

@MadMikeyB
Created April 14, 2016 19:36
Show Gist options
  • Save MadMikeyB/f170275a4af91a0b56f01c329086246c to your computer and use it in GitHub Desktop.
Save MadMikeyB/f170275a4af91a0b56f01c329086246c to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use File;
class MakeViewCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:view {view}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new view with a starter template.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$view = $this->argument('view');
$path = $this->viewPath($view);
$this->createDir($path);
if (File::exists($path))
{
$this->error("File {$path} already exists!");
return;
}
$tpl = "@extends('layouts.app')".PHP_EOL."@section('content')".PHP_EOL."Content".PHP_EOL."@stop";
File::put($path, $tpl);
$this->info("File {$path} created.");
}
/**
* Get the view full path.
*
* @param string $view
*
* @return string
*/
public function viewPath($view)
{
$view = str_replace('.', '/', $view) . '.blade.php';
$path = "resources/views/{$view}";
return $path;
}
/**
* Create view directory if not exists.
*
* @param $path
*/
public function createDir($path)
{
$dir = dirname($path);
if (!file_exists($dir))
{
mkdir($dir, 0777, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment