-
Notifications
You must be signed in to change notification settings - Fork 12
Generators
Chirag Gude edited this page Mar 6, 2015
·
3 revisions
Customize JeffreyWay/Laravel-4-Generators to generate Views
To generate a fully scaffolded resource run the command php artisan generate:scaffold post --fields="title:string, body:text"
Change the view template: \vendor\way\generators\src\Way\Generators\templates\view.txt
or alternatively you can run php artisan generate:publish-templates
so that all template files are published to app
directory
////////////////////////////////////////////////////////////////////////////
INDEX RESOURCE
////////////////////////////////////////////////////////////////////////////
@extends('layouts.master')
@section('content')
{{ link_to_route('$MODEL_NAME$.create', 'Create $MODEL_NAME_SINGULAR$', null, array('class' => 'btn btn-success pull-right')) }}
<h1>$MODEL_NAME$</h1>
@if(count($$MODEL_NAME$) > 0)
@foreach($$MODEL_NAME$ as $$MODEL_NAME_SINGULAR$)
$FIELDS$ - Usage: {{ $$MODEL_NAME_SINGULAR$->field }}
</br>
<a class="btn btn-xs btn-success" href="{{ URL::to('$MODEL_NAME$/' . $$MODEL_NAME_SINGULAR$->id) }}">Read More</a>
<a class="btn btn-xs btn-info" href="{{ URL::to('$MODEL_NAME$/' . $$MODEL_NAME_SINGULAR$->id . '/edit') }}">Edit</a>
<button class="btn btn-xs btn-warning pull-right" data-toggle="modal" data-target="#delete">Delete</button>
{{ HTML::deleteModal('delete','$MODEL_NAME$','$MODEL_NAME_SINGULAR$', $MODEL_NAME_SINGULAR$->id) }}
@endforeach
@else
<p class="text-muted">Get started by creating a $MODEL_NAME_SINGULAR$</p>
@endif
@stop
////////////////////////////////////////////////////////////////////////////
CREATE RESOURCE
////////////////////////////////////////////////////////////////////////////
@extends('layouts.master')
@section('content')
<h1>Create $MODEL_NAME_SINGULAR$</h1>
{{ Form::open(array('route' => '$MODEL_NAME$.store', 'role'=>'form')) }}
$FIELDS$ Usage: {{ Form::textField('field', 'Field Name', 'placeholder-text') }}
<a href='{{URL::previous()}}' class='btn btn-default pull-right'>Cancel</a>
{{ Form::submitField('Save', 'btn btn-primary') }}
{{ Form::close() }}
@stop
/////////////////////////////////////////////////////////////////////////
EDIT RESOURCE
/////////////////////////////////////////////////////////////////////////
@extends('layouts.master')
@section('content')
<h1>Edit $MODEL_NAME_SINGULAR$</h1>
{{ Form::model($$MODEL_NAME_SINGULAR$, array('route' => ['$MODEL_NAME$.update', $$MODEL_NAME_SINGULAR$->id],'method' => 'put')) }}
$FIELDS$ Usage: {{ Form::textField('field', 'Field Name', '') }}
<a href='{{URL::previous()}}' class='btn btn-default pull-right'>Cancel</a>
{{ Form::submitField('Save Changes', 'btn btn-primary') }}
{{ Form::close() }}
@stop
/////////////////////////////////////////////////////////////////////////
SHOW RESOURCE
/////////////////////////////////////////////////////////////////////////
@extends('layouts.master')
@section('content')
{{ link_to_route('$MODEL_NAME$.index', ' All $MODEL_NAME_SINGULAR$', null, array('class'=>'btn btn-primary pull-right')) }}
<h2>{{ $$MODEL_NAME_SINGULAR$->field }} <small>{{ $$MODEL_NAME_SINGULAR$->updated_at->diffForHumans() }}</small></h2>
<hr>
$FIELDS$ Usage: {{ $$MODEL_NAME_SINGULAR$->field }}
<hr>
<a class="btn btn-xs btn-info" href="{{ URL::to('$MODEL_NAME$/' . $$MODEL_NAME_SINGULAR$->id . '/edit') }}">Edit</a>
<button class="btn btn-xs btn-warning pull-right" data-toggle="modal" data-target="#delete">Delete</button>
{{ HTML::deleteModal('delete','$MODEL_NAME$','$MODEL_NAME_SINGULAR$', $MODEL_NAME_SINGULAR$->id) }}
@stop
Alter the Resource Generate Command to pass the Model name and Fields to the View Generator \vendor\way\generators\src\Way\Generators\Commands\ResourceGeneratorCommand.php
protected function callView($resource)
{
$collection = $this->getTableName($resource);
$modelName = $this->getModelName($resource);
$fields = $this->option('fields'); // CPG
if ($this->confirm("Do you want me to create views for this $modelName resource? [yes|no]"))
{
foreach(['index', 'show', 'create', 'edit'] as $viewName)
{
$viewName = "{$collection}.{$viewName}";
//$this->call('generate:view', compact('viewName')); // CPG
$this->call('generate:view', compact('viewName','modelName','fields')); // CPG
}
}
}
Alter View Generator Command to accept the arguments from the Resource Generate Command and generate a view \vendor\way\generators\src\Way\Generators\Commands\ViewGeneratorCommand.php
// CPG
protected function getModelName()
{
$modelName = strtolower(str_plural($this->argument('modelName')));
return $modelName;
}
// CPG
protected function getFieldNames()
{
$fields = $this->argument('fields');
return $fields;
}
/**
* Fetch the template data
*
* @return array
*/
protected function getTemplateData()
{
return [
'PATH' => $this->getFileGenerationPath(),
// CPG
'MODEL_NAME' => $this->getModelName(),
'MODEL_NAME_SINGULAR' => str_singular($this->getModelName()),
'FIELDS' => $this->getFieldNames()
];
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['viewName', InputArgument::REQUIRED, 'The name of the desired view'],
// CPG
['modelName', InputArgument::REQUIRED, 'The name of the desired model'],
['fields', null, InputOption::VALUE_OPTIONAL, 'Fields for the migration'],
];
}
- Run scaffold generator
- Separate out views for Create. Edit, Show, Index
- Change all forms in Create and Edit Views
- Got to Model, add
protected $fillable = ['field-name','field-name'];
- Recheck all
{{ resource->field-name }}
in Index, Edit, Show views
- Command:
php artisan generate:view name-of-view model-name fields
- Example:
php artisan generate:view dog dobberman name:string,breed:string
Seperately generate Index, Create, Show & Edit views (source)
ViewGeneratorCommand
<?php namespace Way\Generators\Commands;
use Illuminate\Support\Facades\File;
//added pluralizer namespace
use Illuminate\Support\Pluralizer;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ViewGeneratorCommand extends GeneratorCommand {
/**
* Fetch the template data
*
* @return array
*/
protected function getTemplateData()
{
// change to create the needed vars to paths
$temp = explode('.', $this->argument('viewName'));
$model = $temp[0];
$models = Pluralizer::plural($model); // posts
$Models = ucwords($models); // Posts
$Model = Pluralizer::singular($Models); // Post
return [
'Model' => $Model,
'Models' => $models,
'model' => $model,
'models' => $models,
];
}
}
config.php
'index_template_path' => 'app/generator/templates/views/index.txt',
'create_template_path' => 'app/generator/templates/views/create.txt',
'edit_template_path' => 'app/generator/templates/views/edit.txt',
'show_template_path' => 'app/generator/templates/views/show.txt',
'form_template_path' => 'app/generator/templates/views/_form.txt',