Creating Views and Controllers
This is second part of the tutorial. In first part we created model and migration. Now we will create controller, request object and views for our Article.
In scaffold
folder create following file app/Http/Controllers/__controller__Controller.php
. As explaind before, this will be used as a controller blueprint.
<?php namespace __$namespace__Http\Controllers;
use App\Http\Requests;
use __$namespace__Http\Requests\__$model__FormRequest;
use __$namespace__Models\__$model__;
use Redirect;
use Session;
class __$controller__Controller extends Controller {
public $__$item__;
public function __'_'.'_'__construct( __$model.' ' __ $__$item__ )
{
$this->__$item__ = $__$item__;
}
public function index()
{
$__$collection.' '__ = $this->__$item__->all();
return view('__$collection__.index',compact('__$collection__'));
}
public function create()
{
$__$item.' '__ = $this->__$item__;
return view('__$collection__.create',compact('__$item__'));
}
public function store(__$model__FormRequest $request )
{
$this->__$item__->create($request->all());
Session::flash('success','You successfully added __$model__');
return Redirect::route('__$collection__.index');
}
public function show($__$item__)
{
return view('__$collection__.show',compact('__$item__'));
}
public function edit($__$item__)
{
return view('__$collection__.edit',compact('__$item__'));
}
public function update(__$model__FormRequest $request, $__$item__)
{
$__$item__->update($request->all());
Session::flash('success','You successfully edit __$model__');
return Redirect::route('__$collection__.index');
}
public function destroy($__$item__)
{
$__$item__->delete();
Session::flash('success','You successfully delete __$model__');
return Redirect::route('__$collection__.index');
}
}
Equivalent of __$controller__
in template files is __controller__
in file path. Note that dollar sign is missing. This gives you a way to use template variables not only for templates but for folders structure also.
We will use this functionality to create views. Create folder structure like this scaffold/resources/views/__collection__
and four files: create.blade.php
, edit.blade.php
, index.blade.php
and show.blade.php
.
Add to index.blade.php
following template:
<h1>All __ ucfirst($collection)__ </h1>
<p>
{!! link_to_route('__$collection__.create', 'Create new __$collection__' ) !!}
</p>
@if(count($__$collection__))
<table>
<thead>
<tr>
__!foreach($fields as $field):__
__!if($field->has('relation')) : __
<th>__ ucfirst($field->get("relation")["name"])__</th>
__!else: __
<th>__ ucfirst($field) __</th>
__!endif;__
__!endforeach;__
</tr>
</thead>
<tbody>
@foreach($__$collection.' '__ as $__$item__)
<tr>
__!foreach($fields as $field):__
<td>{{$__$item__->__$field__}}</td>
__!endforeach;__
<td>{!! link_to_route('__$collection__.edit', 'Edit', array($__$item__->id)) !!}</td>
<td>
{!! Form::open(array('method' => 'DELETE', 'route' => array('__$collection__.destroy', $__$item__->id))) !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<p>You do not have __ ucfirst($singleItem).' '__ in database</p>
@endif
We need to add our new resource to the application routes. Open routes.php
and add:
Route::model('articles', 'App\Models\Article');
Route::resource('articles', 'ArticlesController');
Run generator command php artisan proto user --fields='{"name":{}, "body":{}}' -t scaffold
.
In browser, visit application on route articles
(if you are serving application with php artisan serve visit http://localhost:8000/articles) and you will see index page with You do not have Article in database.
Now we will add create part of the CRUD. Create file resources/views/__collection__/create.blade.php
in scaffold
folder. And create template:
<h1>Create New __ ucfirst($singleItem)__ </h1>
<p>
{!! link_to_route('__$collection__.index', 'All __$collection__') !!}
</p>
{!!Form::model($__$item__,[ 'route' => '__$collection__.store' ] )!!}
__!foreach($fields as $field):__
<div>
{!! Form::label('__$field__','__ ucfirst($field) __:') !!}
{!! Form::text('__$field__', null, array('placeholder' => '__ ucfirst($field) __')) !!}
</div>
__!endforeach;__
<div>
{!! Form::submit('Submit') !!}
</div>
{!!Form::close()!!}
After submit we need validation. Create Request object. scaffold/app/Http/Requests/__model__FormRequest.php
.
In this case __model__
is used as appropriate variation of the article resource.
<?php namespace __$namespace__Http\Requests;
class __$model__FormRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
return [
__!foreach($fields as $field):__
'__$field__' => 'required',
__!endforeach;__
];
}
}
Final folder structure will look like this:
With same principle you can create edit and show templates.