Skip to content

Instantly share code, notes, and snippets.

@chrisforrence
Forked from umefarooq/Laravel 5.2 MakeViewCommad
Last active December 13, 2016 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisforrence/f352347bb8213aa612ff78fb62e6e85b to your computer and use it in GitHub Desktop.
Save chrisforrence/f352347bb8213aa612ff78fb62e6e85b to your computer and use it in GitHub Desktop.
Laravel 5 make:view command

Original: https://gist.github.com/sahibalejandro/1030d43259a6fe95f79f

Installation

Note: This assumes that we are in the Laravel application root folder

  • Copy MakeViewCommand.php in this Gist into ./app/Console/MakeViewCommand.php

  • Edit ./app/Console/Kernel.php and add the command into the $commands array:

      protected $commands = [
          MakeViewCommand::class,
          /* Additional commands */
      ];
    
<?php
/**
* Original: https://gist.github.com/sahibalejandro/1030d43259a6fe95f79f
*
* To install:
* - `php artisan make:command MakeViewCommand`
* - Copy the contents of this file into the created file
* - Edit ./app/Console/Kernel.php and add `MakeViewCommand::class,` to the `$commands` array
*/
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 blade 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;
}
File::put($path, "@extends('layouts.app')" . PHP_EOL . PHP_EOL);
$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, 0755, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment