6.7.2. A Customer Controller

First, we’ll write our Customer controller.

  1. <?php
  2. /*
  3. * Customer controller
  4. */
  5. namespace App\Http\Controllers;
  6. use App\Http\Controllers\Controller;
  7. use App\Customer;
  8. class CustomerController extends Controller
  9. {
  10. /**
  11. * Show customer list
  12. *
  13. * @return Response
  14. */
  15. public function showCustomers()
  16. {
  17. // get the first 20 customers
  18. // sorted alphabetically
  19. $customers = Customer::select()
  20. ->orderBy('NAME')
  21. ->take(20)
  22. ->get();
  23. var_dump($customers);
  24. }
  25. }

Now we have to link the controller methods to the route. For this, add the following line to routes.php (web.php):

  1. 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.

  1. <?php
  2. /*
  3. * Customer Controller
  4. */
  5. namespace App\Http\Controllers;
  6. use App\Http\Controllers\Controller;
  7. use App\Customer;
  8. class CustomerController extends Controller {
  9. /**
  10. * Displays the list of customers
  11. *
  12. * @return Response
  13. */
  14. public function showCustomers() {
  15. // Connect widget for search
  16. $filter = \DataFilter::source(new Customer);
  17. // Search will be by the name of the supplier
  18. $filter->add('NAME', 'Name', 'text');
  19. // Set capture for search button
  20. $filter->submit('Search');
  21. // Add the filter reset button and assign it caption
  22. $filter->reset('Reset');
  23. // Create a grid to display the filtered data
  24. $grid = \DataGrid::source($filter);
  25. // output columns
  26. // Field, label, sorted
  27. $grid->add('NAME', 'Name', true);
  28. $grid->add('ADDRESS', 'Address');
  29. $grid->add('ZIPCODE', 'Zip Code');
  30. $grid->add('PHONE', 'Phone');
  31. // Add buttons to view, edit and delete records
  32. $grid->edit('/customer/edit', 'Edit', 'show|modify|delete');
  33. // Add the Add Customer button
  34. $grid->link('/customer/edit', "Add customer", "TR");
  35. $grid->orderBy('NAME', 'asc');
  36. // set the number of records per page
  37. $grid->paginate(10);
  38. // display the customer template and pass the filter and grid to it
  39. return view('customer', compact('filter', 'grid'));
  40. }
  41. /**
  42. * Add, edit and delete a customer
  43. *
  44. * @return Response
  45. */
  46. public function editCustomer() {
  47. if (\Input::get('do_delete') == 1)
  48. return "not the first";
  49. // create an editor
  50. $edit = \DataEdit::source(new Customer());
  51. // Set title of the dialog, depending on the type of operation
  52. switch ($edit->status) {
  53. case 'create':
  54. $edit->label('Add customer');
  55. break;
  56. case 'modify':
  57. $edit->label('Edit customer');
  58. break;
  59. case 'do_delete':
  60. $edit->label('Delete customer');
  61. break;
  62. case 'show':
  63. $edit->label("Customer's card");
  64. // add a link to go back to the list of customers
  65. $edit->link('customers', 'Back', 'TR');
  66. break;
  67. }
  68. // set that after the operations of adding, editing and deleting,
  69. // you need to return to the list of customers
  70. $edit->back('insert|update|do_delete', 'customers');
  71. // We add editors of a certain type, assign them a label and
  72. // associate them with the attributes of the model
  73. $edit->add('NAME', 'Name', 'text')->rule('required|max:60');
  74. $edit->add('ADDRESS', 'Address', 'textarea')
  75. ->attributes(['rows' => 3])
  76. ->rule('max:250');
  77. $edit->add('ZIPCODE', 'Zip code', 'text')->rule('max:10');
  78. $edit->add('PHONE', 'Phone', 'text')->rule('max:14');
  79. // display the template customer_edit and pass it to the editor
  80. return $edit->view('customer_edit', compact('edit'));
  81. }
  82. }

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:

  1. @extends('example')
  2. @section('title', 'Customers')
  3. @section('body')
  4. <h1>Customers</h1>
  5. <p>
  6. {!! $filter !!}
  7. {!! $grid !!}
  8. </p>
  9. @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.

  1. @extends('master')
  2. @section('title', 'Example of working with Firebird')
  3. @section('body')
  4. <h1>??????</h1>
  5. @if(Session::has('message'))
  6. <div class="alert alert-success">
  7. {!! Session::get('message') !!}
  8. </div>
  9. @endif
  10. <p>Example of working with Firebird.<br/>
  11. </p>
  12. @stop
  13. @section('content')
  14. @include('menu')
  15. @yield('body')
  16. @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.

  1. <nav class="navbar main">
  2. <div class="navbar-header">
  3. <button type="button" class="navbar-toggle"
  4. data-toggle="collapse" data-target=".main-collapse">
  5. <span class="sr-only"></span>
  6. <span class="icon-bar"></span>
  7. <span class="icon-bar"></span>
  8. <span class="icon-bar"></span>
  9. </button>
  10. </div>
  11. <div class="collapse navbar-collapse main-collapse">
  12. <ul class="nav nav-tabs">
  13. <li @if (Request::is('customer*'))
  14. class="active"@endif>{!! link_to("customers", "Customers") !!}</li>
  15. <li @if (Request::is('product*'))
  16. class="active"@endif>{!! link_to("products", "Products") !!}</li>
  17. <li @if (Request::is('invoice*'))
  18. class="active"@endif>{!! link_to("invoices", "Invoices") !!}</li>
  19. </ul>
  20. </div>
  21. </nav>

The master template enables css styles and JavaScript files with libraries.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>@yield('title', 'An example of a Web application on Firebird')</title>
  7. <meta name="description" content="@yield('description',
  8. 'An example of a Web application on Firebird')" />
  9. @section('meta', '')
  10. <link href="http://fonts.googleapis.com/css?family=Bitter" rel="stylesheet"
  11. type="text/css" />
  12. <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"
  13. rel="stylesheet">
  14. <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"
  15. rel="stylesheet">
  16. {!! Rapyd::styles(true) !!}
  17. </head>
  18. <body>
  19. <div id="wrap">
  20. <div class="container">
  21. <br />
  22. <div class="row">
  23. <div class="col-sm-12">
  24. @yield('content')
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. <div id="footer">
  30. </div>
  31. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
  32. </script>
  33. <script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js">
  34. </script>
  35. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.pjax/1.9.6/jquery.pjax.min.js"></script>
  36. <script src="https://cdnjs.cloudflare.com/ajax/libs/riot/2.2.4/riot+compiler.min.js"></script>
  37. {!! Rapyd::scripts() !!}
  38. </body>
  39. </html>

The customer_edit template:

  1. @extends('example')
  2. @section('title', 'Edit customer')
  3. @section('body')
  4. <p>
  5. {!! $edit !!}
  6. </p>
  7. @stop