6.7.2. A Customer Controller
First, we’ll write our Customer controller.
<?php
/*
* Customer controller
*/
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Customer;
class CustomerController extends Controller
{
/**
* Show customer list
*
* @return Response
*/
public function showCustomers()
{
// get the first 20 customers
// sorted alphabetically
$customers = Customer::select()
->orderBy('NAME')
->take(20)
->get();
var_dump($customers);
}
}
Now we have to link the controller methods to the route. For this, add the following line to routes.php
(web.php):
Route::get('/customers', 'CustomerController@showCustomers');
The controller name is separated from the method name with the @ character.
To build a quick interface with grids and edit dialog boxes, we will use the zofe/rapyd
package that was enabled earlier. Classes from the zofe/rapyd
package take up the role of building standard queries to Eloquent ORM models. We will change the customer controller so that it shows data on the grid, allows filtering and record insertions, updates and deletes by way of the edit dialog boxes.
<?php
/*
* Customer Controller
*/
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Customer;
class CustomerController extends Controller {
/**
* Displays the list of customers
*
* @return Response
*/
public function showCustomers() {
// Connect widget for search
$filter = \DataFilter::source(new Customer);
// Search will be by the name of the supplier
$filter->add('NAME', 'Name', 'text');
// Set capture for search button
$filter->submit('Search');
// Add the filter reset button and assign it caption
$filter->reset('Reset');
// Create a grid to display the filtered data
$grid = \DataGrid::source($filter);
// output columns
// Field, label, sorted
$grid->add('NAME', 'Name', true);
$grid->add('ADDRESS', 'Address');
$grid->add('ZIPCODE', 'Zip Code');
$grid->add('PHONE', 'Phone');
// Add buttons to view, edit and delete records
$grid->edit('/customer/edit', 'Edit', 'show|modify|delete');
// Add the Add Customer button
$grid->link('/customer/edit', "Add customer", "TR");
$grid->orderBy('NAME', 'asc');
// set the number of records per page
$grid->paginate(10);
// display the customer template and pass the filter and grid to it
return view('customer', compact('filter', 'grid'));
}
/**
* Add, edit and delete a customer
*
* @return Response
*/
public function editCustomer() {
if (\Input::get('do_delete') == 1)
return "not the first";
// create an editor
$edit = \DataEdit::source(new Customer());
// Set title of the dialog, depending on the type of operation
switch ($edit->status) {
case 'create':
$edit->label('Add customer');
break;
case 'modify':
$edit->label('Edit customer');
break;
case 'do_delete':
$edit->label('Delete customer');
break;
case 'show':
$edit->label("Customer's card");
// add a link to go back to the list of customers
$edit->link('customers', 'Back', 'TR');
break;
}
// set that after the operations of adding, editing and deleting,
// you need to return to the list of customers
$edit->back('insert|update|do_delete', 'customers');
// We add editors of a certain type, assign them a label and
// associate them with the attributes of the model
$edit->add('NAME', 'Name', 'text')->rule('required|max:60');
$edit->add('ADDRESS', 'Address', 'textarea')
->attributes(['rows' => 3])
->rule('max:250');
$edit->add('ZIPCODE', 'Zip code', 'text')->rule('max:10');
$edit->add('PHONE', 'Phone', 'text')->rule('max:14');
// display the template customer_edit and pass it to the editor
return $edit->view('customer_edit', compact('edit'));
}
}
blade Templates
By default, Laravel uses the blade template engine. The view
function finds the necessary template in the resources/views
directory, makes the necessary changes to it and returns the text of the HTML page, at the same time passing to it any variables that are supplied in the template. You can find the description of the blade template syntax at https://laravel.com/docs/5.2/blade.
The Template for Displaying Customers
The template for displaying customers looks like this:
@extends('example')
@section('title', 'Customers')
@section('body')
<h1>Customers</h1>
<p>
{!! $filter !!}
{!! $grid !!}
</p>
@stop
This template is inherited from the example template and redefines its body section. The $filter
and $grid
variables contain the HTML code for filtering and displaying data on the grid. The example template is common for all pages.
@extends('master')
@section('title', 'Example of working with Firebird')
@section('body')
<h1>??????</h1>
@if(Session::has('message'))
<div class="alert alert-success">
{!! Session::get('message') !!}
</div>
@endif
<p>Example of working with Firebird.<br/>
</p>
@stop
@section('content')
@include('menu')
@yield('body')
@stop
This template is itself inherited from the master template and also enables the menu template. The menu is quite simple and consists of three items: Customers, Products and Invoices.
<nav class="navbar main">
<div class="navbar-header">
<button type="button" class="navbar-toggle"
data-toggle="collapse" data-target=".main-collapse">
<span class="sr-only"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse main-collapse">
<ul class="nav nav-tabs">
<li @if (Request::is('customer*'))
class="active"@endif>{!! link_to("customers", "Customers") !!}</li>
<li @if (Request::is('product*'))
class="active"@endif>{!! link_to("products", "Products") !!}</li>
<li @if (Request::is('invoice*'))
class="active"@endif>{!! link_to("invoices", "Invoices") !!}</li>
</ul>
</div>
</nav>
The master template enables css styles and JavaScript files with libraries.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@yield('title', 'An example of a Web application on Firebird')</title>
<meta name="description" content="@yield('description',
'An example of a Web application on Firebird')" />
@section('meta', '')
<link href="http://fonts.googleapis.com/css?family=Bitter" rel="stylesheet"
type="text/css" />
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"
rel="stylesheet">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"
rel="stylesheet">
{!! Rapyd::styles(true) !!}
</head>
<body>
<div id="wrap">
<div class="container">
<br />
<div class="row">
<div class="col-sm-12">
@yield('content')
</div>
</div>
</div>
</div>
<div id="footer">
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.pjax/1.9.6/jquery.pjax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/riot/2.2.4/riot+compiler.min.js"></script>
{!! Rapyd::scripts() !!}
</body>
</html>
The customer_edit template:
@extends('example')
@section('title', 'Edit customer')
@section('body')
<p>
{!! $edit !!}
</p>
@stop